|
| 1 | +/* |
| 2 | + * Telif hakkı 2017 Türkiye Bilimsel ve Teknolojik Araştırma Kurumu. |
| 3 | + * Tüm haklar saklıdır. |
| 4 | + * |
| 5 | + * BU TELİF HAKKI DOSYA ÜST BİLGİSİNİ DEĞİŞTİRMEYİNİZ VEYA SİLMEYİNİZ. |
| 6 | + * |
| 7 | + * Bu kaynak kod sadece TÜBİTAK için TYBS Projesi kapsamında geliştirilmiştir; |
| 8 | + * değişiklik yapma, başka amaçlarda kullanılmak üzere kısmen veya tamamını |
| 9 | + * kopyalama ve/veya dağıtım yapma hakkınız bulunmamaktadır. |
| 10 | + * |
| 11 | + * Daha fazla bilgi ve sorularınız için TÜBİTAK ile iletişime geçiniz. |
| 12 | + */ |
| 13 | +package com.fd.jpa.config; |
| 14 | + |
| 15 | +import com.fd.jpa.audit.SpringSecurityAuditorAware; |
| 16 | +import com.mchange.v2.c3p0.ComboPooledDataSource; |
| 17 | +import org.hibernate.jpa.HibernatePersistenceProvider; |
| 18 | +import org.springframework.beans.factory.annotation.Autowired; |
| 19 | +import org.springframework.context.annotation.Bean; |
| 20 | +import org.springframework.context.annotation.Configuration; |
| 21 | +import org.springframework.context.annotation.PropertySource; |
| 22 | +import org.springframework.core.env.Environment; |
| 23 | +import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor; |
| 24 | +import org.springframework.data.domain.AuditorAware; |
| 25 | +import org.springframework.data.jpa.repository.config.EnableJpaAuditing; |
| 26 | +import org.springframework.data.jpa.repository.config.EnableJpaRepositories; |
| 27 | +import org.springframework.orm.jpa.JpaTransactionManager; |
| 28 | +import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; |
| 29 | +import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; |
| 30 | +import org.springframework.transaction.annotation.EnableTransactionManagement; |
| 31 | + |
| 32 | +import java.beans.PropertyVetoException; |
| 33 | +import java.util.Properties; |
| 34 | + |
| 35 | +/** |
| 36 | + * @author furkan.danismaz |
| 37 | + * 10/09/2018 12:05 |
| 38 | + */ |
| 39 | +@Configuration |
| 40 | +@PropertySource("database.properties") |
| 41 | +@EnableTransactionManagement |
| 42 | +@EnableJpaRepositories(basePackages = "${hibernate.repositories_scan}") |
| 43 | +@EnableJpaAuditing(auditorAwareRef = "auditorAware") |
| 44 | +public class DatabaseConfiguration { |
| 45 | + |
| 46 | + @Autowired |
| 47 | + private Environment environment; |
| 48 | + |
| 49 | + @Bean |
| 50 | + public ComboPooledDataSource dataSource() throws PropertyVetoException { |
| 51 | + ComboPooledDataSource dataSource = new ComboPooledDataSource(); |
| 52 | + dataSource.setDriverClass(this.environment.getProperty("hibernate.driver")); |
| 53 | + dataSource.setJdbcUrl(this.environment.getProperty("hibernate.db_url")); |
| 54 | + dataSource.setUser(this.environment.getProperty("hibernate.db_user")); |
| 55 | + dataSource.setPassword(this.environment.getProperty("hibernate.db_password")); |
| 56 | + |
| 57 | + // C3P0 Connection Pool Settings |
| 58 | + dataSource.setAcquireIncrement(Integer.parseInt(this.environment.getProperty("hibernate.c3p0.acquire_increment"))); |
| 59 | + dataSource.setInitialPoolSize(Integer.parseInt(this.environment.getProperty("hibernate.c3p0.initialPoolSize"))); |
| 60 | + dataSource.setMinPoolSize(Integer.parseInt(this.environment.getProperty("hibernate.c3p0.minPoolSize"))); |
| 61 | + dataSource.setMaxPoolSize(Integer.parseInt(this.environment.getProperty("hibernate.c3p0.maxPoolSize"))); |
| 62 | + dataSource.setMaxIdleTime(Integer.parseInt(this.environment.getProperty("hibernate.c3p0.maxIdleTime"))); |
| 63 | + dataSource.setIdleConnectionTestPeriod(Integer.parseInt(this.environment.getProperty("hibernate.c3p0.idleConnectionTestPeriod"))); |
| 64 | + dataSource.setMaxStatements(Integer.parseInt(this.environment.getProperty("hibernate.c3p0.maxStatements"))); |
| 65 | + |
| 66 | + return dataSource; |
| 67 | + } |
| 68 | + |
| 69 | + @Bean |
| 70 | + public LocalContainerEntityManagerFactoryBean entityManagerFactory() throws PropertyVetoException { |
| 71 | + HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); |
| 72 | + vendorAdapter.setGenerateDdl(true); |
| 73 | + |
| 74 | + LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean(); |
| 75 | + factory.setJpaVendorAdapter(vendorAdapter); |
| 76 | + factory.setPackagesToScan(this.environment.getProperty("hibernate.entities_scan")); |
| 77 | + factory.setDataSource(this.dataSource()); |
| 78 | + factory.setPersistenceProviderClass(HibernatePersistenceProvider.class); |
| 79 | + factory.setJpaProperties(this.hibernateProperties()); |
| 80 | + |
| 81 | + return factory; |
| 82 | + } |
| 83 | + |
| 84 | + @Bean |
| 85 | + public PersistenceExceptionTranslationPostProcessor exceptionTranslation(){ |
| 86 | + return new PersistenceExceptionTranslationPostProcessor(); |
| 87 | + } |
| 88 | + |
| 89 | + @Bean |
| 90 | + public JpaTransactionManager transactionManager() throws PropertyVetoException { |
| 91 | + JpaTransactionManager transactionManager = new JpaTransactionManager(); |
| 92 | + transactionManager.setEntityManagerFactory(this.entityManagerFactory().getObject()); |
| 93 | + return transactionManager; |
| 94 | + } |
| 95 | + |
| 96 | + @Bean |
| 97 | + public AuditorAware<String> auditorAware() { |
| 98 | + return new SpringSecurityAuditorAware(); |
| 99 | + } |
| 100 | + |
| 101 | + private Properties hibernateProperties() { |
| 102 | + Properties properties = new Properties(); |
| 103 | + properties.put("hibernate.dialect", this.environment.getProperty("hibernate.dialect")); |
| 104 | + properties.put("hibernate.show_sql", this.environment.getProperty("hibernate.show_sql")); |
| 105 | + properties.put("hibernate.format_sql", this.environment.getProperty("hibernate.format_sql")); |
| 106 | + properties.put("hibernate.hbm2ddl.auto", this.environment.getProperty("hibernate.hbm2ddl_auto")); |
| 107 | + properties.put("org.hibernate.envers.audit_table_suffix", this.environment.getProperty("hibernate.audit_table_suffix")); |
| 108 | + properties.put("org.hibernate.envers.audit_strategy", this.environment.getProperty("hibernate.audit_strategy")); |
| 109 | + return properties; |
| 110 | + } |
| 111 | +} |
0 commit comments