Clover icon

sunshower-base

  1. Project Clover database Fri Apr 6 2018 03:41:27 UTC
  2. Package io.sunshower.persist.hibernate

File HibernateConfigurer.java

 

Coverage histogram

../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

8
52
14
1
168
144
18
0.35
3.71
14
1.29

Classes

Class Line # Actions
HibernateConfigurer 28 52 18
0.918918991.9%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    package io.sunshower.persist.hibernate;
2   
3    import com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionManagerImple;
4    import com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionSynchronizationRegistryImple;
5    import com.arjuna.ats.internal.jta.transaction.arjunacore.UserTransactionImple;
6    import io.sunshower.persist.validation.ModelValidator;
7    import io.sunshower.persistence.PersistenceUnit;
8    import java.util.Properties;
9    import java.util.concurrent.TimeUnit;
10    import java.util.logging.Logger;
11    import javax.persistence.EntityManagerFactory;
12    import javax.sql.DataSource;
13    import javax.transaction.TransactionManager;
14    import javax.transaction.TransactionSynchronizationRegistry;
15    import javax.transaction.UserTransaction;
16    import org.apache.ignite.Ignite;
17    import org.apache.ignite.configuration.CacheConfiguration;
18    import org.cfg4j.provider.ConfigurationProvider;
19    import org.hibernate.search.jpa.FullTextEntityManager;
20    import org.hibernate.search.jpa.Search;
21    import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
22    import org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor;
23    import org.springframework.orm.jpa.vendor.Database;
24    import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
25    import org.springframework.transaction.PlatformTransactionManager;
26    import org.springframework.transaction.jta.JtaTransactionManager;
27   
 
28    public class HibernateConfigurer {
29    private static final String STANDARD_QUERY_CACHE_NAME =
30    "org.hibernate.cache.internal.StandardQueryCache";
31    private static final String UPDATE_TIMESTAMPS_CACHE_NAME =
32    "org.hibernate.cache.spi.UpdateTimestampsCache";
33   
34    static final Logger log = Logger.getLogger(HibernateConfiguration.class.getName());
35   
 
36  4 toggle protected HibernateProviderConfigurationSource hibernateProviderConfigurationSource(
37    DataDefinitionLanguage ddl,
38    SearchConfiguration searchConfiguration,
39    HibernateDialectProperties props,
40    HibernateCacheConfiguration cacheConfiguration) {
41  4 return new HibernateProviderConfigurationSource(
42    ddl, searchConfiguration, props, cacheConfiguration);
43    }
44   
 
45  4 toggle public DataDefinitionLanguage dataDefinitionLanguage(ConfigurationProvider source) {
46  4 return source.bind("jpa.provider.ddl", DataDefinitionLanguage.class);
47    }
48   
 
49  4 toggle public HibernateCacheConfiguration hibernateCacheConfiguration(ConfigurationProvider source) {
50  4 return source.bind("jpa.provider.cache", HibernateCacheConfiguration.class);
51    }
52   
 
53  4 toggle public SearchConfiguration searchConfiguration(ConfigurationProvider source) {
54  4 return source.bind("jpa.provider.search", SearchConfiguration.class);
55    }
56   
 
57  4 toggle public HibernateDialectProperties hibernateDialectProperties(ConfigurationProvider source) {
58  4 return source.bind("jpa.provider", HibernateDialectProperties.class);
59    }
60   
 
61  4 toggle public UserTransaction userTransaction() {
62  4 return new UserTransactionImple();
63    }
64   
 
65  4 toggle public TransactionManager jtaTransactionManager() {
66  4 return new TransactionManagerImple();
67    }
68   
 
69  4 toggle public TransactionSynchronizationRegistry transactionSynchronizationRegistry() {
70  4 return new TransactionSynchronizationRegistryImple();
71    }
72   
 
73  4 toggle public PlatformTransactionManager transactionManager(
74    EntityManagerFactory entityManagerFactory,
75    DataSource dataSource,
76    UserTransaction userTransaction,
77    TransactionManager txManager,
78    TransactionSynchronizationRegistry registry) {
79  4 final JtaTransactionManager transactionManager = new JtaTransactionManager();
80  4 transactionManager.setUserTransaction(userTransaction);
81  4 transactionManager.setTransactionManager(txManager);
82  4 transactionManager.setAllowCustomIsolationLevels(true);
83  4 transactionManager.setDefaultTimeout((int) TimeUnit.MINUTES.toMillis(20));
84   
85  4 transactionManager.setTransactionSynchronizationRegistry(registry);
86  4 return transactionManager;
87    }
88   
 
89  3 toggle public PersistenceAnnotationBeanPostProcessor persistenceAnnotationBeanPostProcessor() {
90  3 return new PersistenceAnnotationBeanPostProcessor();
91    }
92   
 
93  4 toggle public ModelValidator modelValidator() {
94  4 return new ModelValidator();
95    }
96   
 
97  4 toggle public LocalContainerEntityManagerFactoryBean entityManagerFactory(
98    DataSource dataSource,
99    HibernateProviderConfigurationSource source,
100    PersistenceUnit persistenceConfiguration,
101    ConfigurationProvider provider,
102    Ignite ignite) {
103  4 LocalContainerEntityManagerFactoryBean entityManagerFactoryBean =
104    new LocalContainerEntityManagerFactoryBean();
105  4 entityManagerFactoryBean.setPersistenceUnitName("default-persistence-unit");
106  4 entityManagerFactoryBean.setJtaDataSource(dataSource);
107  4 final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
108  4 vendorAdapter.setDatabase(Database.POSTGRESQL);
109  4 vendorAdapter.setDatabasePlatform("org.hibernate.dialect.PostgreSQL94Dialect");
110  4 entityManagerFactoryBean.setJpaVendorAdapter(vendorAdapter);
111  4 entityManagerFactoryBean.setPackagesToScan(persistenceConfiguration.getScannedPackages());
112   
113  4 Properties properties = Configurations.toNative(provider, source);
114  4 configureCache(ignite, properties, provider, source.getCache(), source);
115  4 entityManagerFactoryBean.setJpaProperties(properties);
116  4 return entityManagerFactoryBean;
117    }
118   
 
119  4 toggle protected void configureCache(
120    Ignite ignite,
121    Properties jpaProperties,
122    ConfigurationProvider cfgProvider,
123    HibernateCacheConfiguration cache,
124    HibernateProviderConfigurationSource source) {
125  4 HibernateDialectProperties provider = Configurations.getProvider(cfgProvider);
126  4 if (provider == null) {
127  4 log.info("No L2 Cache configured");
128    }
129   
130  4 if (cache == null) {
131  0 log.info("No L2 Cache configured");
132    } else {
133  4 if (!cache.enabled()) {
134  0 log.info("L2 Cache is disabled");
135    } else {
136  4 String cacheProvider = cache.provider();
137  4 log.info("L2 Cache is enabled");
138  4 log.info("Cache provider: '" + cacheProvider + "'");
139  4 jpaProperties.put("hibernate.cache.use_second_level_cache", true);
140    }
141  4 if (cache.enableQueryCache()) {
142  4 log.info("Query cache is enabled");
143  4 ignite.getOrCreateCache(new CacheConfiguration<>(UPDATE_TIMESTAMPS_CACHE_NAME));
144  4 ignite.getOrCreateCache(new CacheConfiguration<>(STANDARD_QUERY_CACHE_NAME));
145  4 jpaProperties.put("hibernate.cache.use_query_cache", true);
146    }
147  4 jpaProperties.put("hibernate.cache.region.factory_class", cache.regionFactory());
148  4 jpaProperties.put("org.apache.ignite.hibernate.grid_name", cache.fabricName());
149    }
150   
151  4 jpaProperties.put(
152    "hibernate.transaction.factory_class", "org.hibernate.transaction.JTATransactionFactory");
153   
154  4 jpaProperties.put(
155    "hibernate.transaction.jta.platform",
156    "org.hibernate.service.jta.platform.internal.JBossStandAloneJtaPlatform");
157  4 jpaProperties.put(
158    "hibernate.transaction.manager_lookup_class",
159    "org.hibernate.transaction.JBossTransactionManagerLookup");
160   
161    // jpaProperties.put("hibernate.connection.release_mode", "on_close");
162  4 jpaProperties.put("hibernate.connection.acquisition_mode", "immediate");
163    }
164   
 
165  4 toggle public FullTextEntityManager entityManager(EntityManagerFactory entityManagerFactory) {
166  4 return Search.getFullTextEntityManager(entityManagerFactory.createEntityManager());
167    }
168    }