JpaStoreConfigurationBuilder and unpersisted data in LocalCache with embedded DBs like Derby or Hypersonic?
a.niemeyer Dec 8, 2016 4:57 PMHi,
I tried hard to get a running configuration - and failed:
For a local (database) caching in a staging service the entities should be written and persisted in sync into an embedded database permanently with less memory consumtion as possible.
The issue is that no (entity) data is written into the database but data tables were created in-process.
Setup: AppServer WildFly Full 9.0.1.Final with infinispan 7.2.3 with infinispan-cachestore-jpa-7.2.3.Final.jar release.
The LocalCache itself is valid, I did some debugging and logging, entities are stored in the cache and could read out back from memory.
The database and tables were managed by JTA and Hibernate, defined in a persistence.xml; the hibernate managed datasource is probably independent from the programmatically cache setup in the ServiceBean.
I guess I did some misconfigurations and before I give it a new try with an updated Wildfly 10 release, I would like to know the reasons first.
There are dozens of configuration options available and probably one would do the trick... or a wrong persistence configuration is in charge or misunderstandings....
Thank you in advance for any pieces of help!
Kind regards,
Andreas
The planned architecture is simple:
1.) using a SingletonBean bean for providing the cache service
@Startup
@Singleton
@LocalBean
@TransactionManagement(TransactionManagementType.BEAN)
public class LocalCacheService
private Logger log = LoggerFactory.getLogger(LocalCacheService.class);
private EmbeddedCacheManager cacheManager;
private Cache<String, TestCacheEntityXY> jpaCache;
@PostConstruct
private void init() {
GlobalConfiguration globalConfig = new GlobalConfigurationBuilder().globalJmxStatistics().allowDuplicateDomains(true).jmxDomain("domain").build();
// playing around with different property values - nothing helped
Configuration cacheConfig = new ConfigurationBuilder().persistence()
.passivation(true)
.addStore(JpaStoreConfigurationBuilder.class)
.persistenceUnitName("TestCacheUnit")
.entityClass(TestCacheEntityXY.class)
.purgeOnStartup(false)
.preload(true)
.shared(true)
.jmxStatistics()
.enabled(false)
.clustering()
.cacheMode(CacheMode.LOCAL)
.transaction()
.transactionMode(TransactionMode.TRANSACTIONAL)
.autoCommit(false)
.transactionManagerLookup(new GenericTransactionManagerLookup())
.build();
// StoreConfiguration storeConfiguration = cacheConfig.persistence().stores().get(0);
// JpaStoreConfiguration jpaCacheLoaderConfig = (JpaStoreConfiguration) storeConfiguration;
cacheManager = new DefaultCacheManager(globalConfig);
cacheManager.defineConfiguration("userCache", cacheConfig);
cacheManager.start();
jpaCache = cacheManager.getCache();
log.info("start LocalCacheService bean with cache size: " + jpaCache.size());
try {
utx.begin();
for (int i = 0; i < 10000; i++) {
jpaCache.put(i + "", new TestCacheEntityXY(i + "", i + "@test.de"));
}
utx.commit();
} catch (Exception e) {
if (utx != null) {
try {
utx.rollback();
} catch (Exception e1) {
}
}
}
log.info(" LocalCacheService bean with cache size after inserts: " + jpaCache.size());
}
}
@PreDestroy
private void stop() {
log.info("stop LocalCacheService bean with cache size: " + jpaCache.size());
jpaCache.stop();
cacheManager.stop();
}
2.) definition of persistence unit
<persistence-unit name="TestCacheUnit" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:jboss/datasources/Test-Cache</jta-data-source>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<class>de.xy.TestCacheEntityXY</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.DerbyTenSevenDialect"/>
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.show_sql" value="true"/>
</properties>
</persistence-unit>
3.) separated datasource definition
<datasources xmlns="http://www.jboss.org/ironjacamar/schema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.jboss.org/ironjacamar/schema http://docs.jboss.org/ironjacamar/schema/datasources_1_0.xsd">
<datasource jta="true" jndi-name="java:jboss/datasources/Test-Cache" pool-name="Test-CacheDSPool" enabled="true" use-ccm="true" use-java-context="true">
<connection-url>jdbc:derby:${jboss.server.data.dir}/derby/TestCache;create=true</connection-url>
<driver>derby</driver>
<security>
<user-name>sa</user-name>
<password>sa</password>
</security>
</datasource>
</datasources>
4.) standalone.xml driver configuration
<drivers>
<driver name="derby" module="org.apache.derby">
<driver-class>org.apache.derby.jdbc.EmbeddedDriver</driver-class>
</driver>
</drivers>