JPA 2.0 Ehcache configuration for L2 cache throwing Error - jpa

I am trying to configure Ehcache for JPA 2.0.
first i have setup following into persistance.xml
<property name="hibernate.cache.provider_class" value="net.sf.ehcache.hibernate.SingletonEhCacheProvider"/>
<property name="hibernate.cache.use_query_cache" value="true"/>
<property name="hibernate.cache.use_second_level_cache" value="true"/>
<property name="hibernate.generate_statistics" value="true"/>
I have ehcache.xml in my class path.
configure #Cacheable(true) to my #Entity class.
I am getting following error.
net.sf.ehcache.config.InvalidConfigurationException: There is one error in your configuration:
* Cache 'net.sf.ehcache.constructs.asynchronous.MessageCache' error: If your CacheManager has no maxBytesLocalHeap set, you need to either set maxEntriesLocalHeap or maxBytesLocalHeap at the Cache level
ehcache.xml
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true"
monitoring="autodetect" dynamicConfig="true">
<!-- By default, Ehcache stored the cached files in temp folder. -->
<!-- <diskStore path="java.io.tmpdir" /> -->
<!-- Ask Ehcache to store cache in this path -->
<diskStore path="c:\\cache" />
<!-- Sample cache named cache1
This cache contains a maximum in memory of 10000 elements, and will expire
an element if it is idle for more than 5 minutes and lives for more than
10 minutes.
If there are more than 10000 elements it will overflow to the
disk cache, which in this configuration will go to wherever java.io.tmp is
defined on your system. On a standard Linux system this will be /tmp" -->
<Cache name = "com.test.myDataDE"
maxEntriesLocalHeap="10000"
maxEntriesLocalDisk="1000"
eternal="false"
diskSpoolBufferSizeMB="20"
timeToIdleSeconds="300" timeToLiveSeconds="600"
memoryStoreEvictionPolicy="LFU"
transactionalMode="off">
<persistence strategy="localTempSwap" />
</Cache>
</ehcache>
Please help me understand any mistake I am doing?

I had the same problem today, solved it by adding:
maxBytesLocalHeap="100M"
and removing:
maxEntriesLocalHeap
in my ehcache.xml configuration.

Without your ehcache.xml content, it is hard to be definitive.
However, given the error message, I believe the issue is that you did not specify a heap size to your cache, either in entries or in bytes.

Related

How to define - environment specifc mongo db configuration in play framework with JpaApi?

I am working on a project where I am using play framework along with mongo db. As of now I have hardcoded the value for local db connection in persistence.xml file and given the jpa.default value as persistenceUnitName and I am using the play's JpaApi for the db operations (which inherently uses the entity manager).
I am not able to identify how to define environment (prod, dev, stage) specific db properties like host, url etc. in application.conf or any other file.
application.conf entry - jpa.default=my-local-jpa
<?xml version="1.0" encoding="UTF-8"?>
<persistence
xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="my-local-jpa" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ogm.jpa.HibernateOgmPersistence</provider>
<non-jta-data-source>DefaultDS</non-jta-data-source>
<properties>
<property name="hibernate.ogm.datastore.provider"
value="org.hibernate.ogm.datastore.mongodb.impl.MongoDBDatastoreProvider"/>
<property name="hibernate.ogm.datastore.host"
value="127.0.0.1"/>
<property name="hibernate.ogm.datastore.port" value="27017"/>
<property name="hibernate.ogm.datastore.database" value="my_db"/>
<property name="hibernate.ogm.datastore.safe" value="true"/>
<property name="hibernate.ogm.datastore.create_database" value="true" />
</properties>
</persistence-unit>
</persistence>
There would be different solutions. It depends on your environment.
If you are using WildFly / JEE container, you can configure a WildFly NoSQL subsystem, providing there the references to the remote datastore. It would be the equivalent of a SQL datasource, but for a NoSQL datastore. See Using WildFly NoSQL
If you are using a web container, there would be different strategies.
You can create different war(s), one for each environment, for instance using maven profiles.
Alternatively, you can configure your Spring context in order to use an external property file. See this question.
If you deploy it in a PASS, such as OpenShift, you can mount the persistence.xml file as a config map. See Config Map - OpenShift doc

Configure Spring batch admin to use db2 database

In order to configure spring batch admin UI to use db2 database, I referred the Admin UI documentation which says "launch the application with a system property -DENVIRONMENT=[type]." I understand that "-DENVIRONMENT=db2" should be kept in some file. I tried by keeping in batch-default.properties file, but that did not work. Since I am using WLP(liberty server), tried by keeping in server.xml file, no help. Still in the console I see env-context.xml file from batch admin is still loading batch-hsql.properties file(default configuration).
My job is written using Spring Boot so I put property, ENVIRONMENT=db2 in application.properties and add a new file - batch-db2.properties at same location as application.properties.
Few compulsory properties will be needed there like - you need to try an experiment,
batch.job.configuration.package=
batch.drop.script=classpath*:/org/springframework/batch/core/schema-drop-db2.sql
batch.schema.script=
batch.business.schema.script=
#Copied from batch.properties of spring-batch-admin-manager API project
batch.jdbc.testWhileIdle=false
batch.jdbc.validationQuery=
batch.data.source.init=false
batch.job.configuration.file.dir=target/config
batch.job.service.reaper.interval=60000
batch.files.upload-dir=/sba/input
I had put DB connection information too but later I moved to JNDI by overriding file - data-source-context.xml in META-INF\spring\batch\override like below,
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="ConnectionPool" />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>
ConnectionPool is db connection pool JNDI name from server.
Keeping configurations in your code lets you freely move your app to different servers without asking for server specific configurations first.
Not really familiar with liberty server, but the link below says that system properties need to be added to jvm.options file. See link below :
https://www.ibm.com/support/knowledgecenter/en/SSEQTP_liberty/com.ibm.websphere.wlp.doc/ae/twlp_admin_customvars.html

Illogical Results from Cucumer/Arquillian Test with H2

I am having an issue where I am attempting to create a Cucumber/Arquillian test for a new service that performs a batch update with JPQL. Everything seems to work correctly, except my #Then code validating the change.
I am setting up my test data in my feature file and it gets added to the H2 database that is created in memory for each test. When the batch update runs it reports back the expected update count based on that data. But when I retrieve one of the objects that should have been updated, the data on that object appears to be unchanged.
Please note: When I execute the service call in my application against our Oracle database it works correctly and the table is updated as expected. The problem seems to be with caching on the H2 in memory database.
My datasource that gets deployed to JBoss by Arquillian is:
<datasource enabled="true"
jndi-name="jdbc/arquillian"
pool-name="ArquillianEmbeddedH2Pool">
<connection-url>
jdbc:h2:mem:arquillian;DB_CLOSE_DELAY=-1;INIT=CREATE SCHEMA IF NOT EXISTS TEST_DB
</connection-url>
<driver>h2</driver>
</datasource>
My Cucumber test defines #PersistenceContext(unitName = "localH2-testDB")
My persistence.xml contains:
<persistence-unit name="localH2-testDB">
<jta-data-source>jdbc/arquillian</jta-data-source>
<properties>
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<property name="hibernate.cache.use_second_level_cache" value="false"/>
<property name="hibernate.cache.use_query_cache" value="false"/>
</properties>
</persistence-unit>
I don't know what other settings for the H2 database I can change to try and eliminate any caching.
It turns out the problem is inherent to how batch updates are handled in JPA. They do NOT update the persistence cache as one might expect. So the tables were updated correctly, but when the object was re-queried, it didn't have the updates. This is why my test was failing (everything was inside the same transaction) and my deployed code worked (separate transactions performing the update and re-querying the data).

Access to Derby database from another project using EclipseLink

I have a dynamic web project that creates and writes Derby database. Everything works fine. Here is the persistence.xml I used:
<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="alerts" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>test.Alert</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver" />
<property name="javax.persistence.jdbc.url" value="jdbc:derby:databases/bDb;create=true" />
<property name="javax.persistence.jdbc.user" value="user" />
<property name="javax.persistence.jdbc.password" value="user-pwd" />
<!-- EclipseLink should create the database schema automatically -->
<property name="eclipselink.ddl-generation" value="create-tables" />
<property name="eclipselink.ddl-generation.output-mode"
value="database" />
<property name="eclipselink.logging.level" value="FINEST" />
<property name="eclipselink.logging.level.sql" value="FINEST" />
</properties>
</persistence-unit>
Now I would like to create a java project that reads the same database. So I use the same persistence.xml (except that I change "create=true" to "create=false" since I don't want to create a new table). but I got this error:
[EL Severe]: ejb: 2014-12-28 22:27:20.379--ServerSession(1488953836)--Thread(Thread[DefaultQuartzScheduler_Worker-7,5,main])--Local Exception Stack:
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLException: Database 'databases/bDb' not found.
Error Code: 40000
at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:331)
at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:326)
at org.eclipse.persistence.sessions.DefaultConnector.connect(DefaultConnector.java:138)
at org.eclipse.persistence.sessions.DatasourceLogin.connectToDatasource(DatasourceLogin.java:162)
at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.setOrDetectDatasource(DatabaseSessionImpl.java:204)
at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.loginAndDetectDatasource(DatabaseSessionImpl.java:741)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryProvider.login(EntityManagerFactoryProvider.java:239)
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:685)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.getAbstractSession(EntityManagerFactoryDelegate.java:204)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.createEntityManagerImpl(EntityManagerFactoryDelegate.java:304)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManagerImpl(EntityManagerFactoryImpl.java:336)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:302)
If I use "create=true", it will create a new database instance (it is different from the one I created from the web project). How do I fix this problem? Thanks a lot!
Since you're using a relative filename in your jdbc.url (databases/bDb), Derby is looking for the database relative to your application start directory.
And your two separate applications have two separate directories in which they are being run.
You could address this in a number of ways:
Arrange for each application to be run from the same directory
Use an absolute database path name, not a relative one, in your jdb.url value
Use the Derby Network Server configuration, not the Derby Embedded configuration
And there are undoubtedly other approaches.

Terracotta and shiro

I want to use terracotta + Shiro in order to implement SSO. When I try to test it, I have the error:
The configuration data in the base configuration from server at 'localhost:9510' does not obey the Terracotta schema:
[0]: Line 4, column 5: Expected elements 'tc-properties servers clients' instead of 'system' here in element tc-config#http://www.terracotta.org/config
[1]: Line 16, column 13: Expected elements 'offheap authentication http-authentication data-backup tsa-port jmx-port tsa-group-port security' instead of 'statistics' here in element server
[2]: Line 19, column 13: Expected elements 'offheap authentication http-authentication data-backup tsa-port jmx-port tsa-group-port security' instead of 'dso-port' here in element server
[3]: Line 21, column 13: Expected elements 'offheap authentication http-authentication data-backup tsa-port tsa-group-port security' instead of 'l2-group-port' here in element server
[4]: Line 32, column 13: Expected elements 'offheap http-authentication data-backup tsa-port tsa-group-port security' instead of 'dso' here in element server
[5]: Line 50, column 9: Expected elements 'server mirror-group update-check garbage-collection restartable client-reconnect-window' instead of 'mirror-groups' here in element servers
[6]: Line 63, column 9: Expected elements 'server mirror-group update-check garbage-collection restartable client-reconnect-window' instead of 'ha' here in element servers
[7]: Line 86, column 5: Element not allowed: application in element tc-config#http://www.terracotta.org/config
Could anyone helps me to find what's wrong?
Here is my configuration:
ehcache.xml on the tc client:
<ehcache name="Plugin_Ehcache"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd"
updateCheck="false" monitoring="autodetect"
dynamicConfig="false">
<diskStore path="java.io.tmpdir/shiro-ehcache"/>
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="false"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120">
<terracotta/>
</defaultCache>
<cache name="shiro-activeSessionCache"
maxElementsInMemory="10000"
eternal="true"
timeToLiveSeconds="0"
timeToIdleSeconds="0"
diskPersistent="false"
overflowToDisk="false"
diskExpiryThreadIntervalSeconds="600">
<terracotta/>
</cache>
<terracottaConfig url="localhost:9510"/>
</ehcache>
tc-config.xml on the tc server
<tc:tc-config xsi:schemaLocation="http://www.terracotta.org/schema/terracotta-8.xsd"
xmlns:tc="http://www.terracotta.org/config"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<tc-properties>
<property name="l2.l1reconnect.enabled" value="true"/>
<property name="l2.l1reconnect.timeout.millis" value="5000"/>
<property name="sigar.enabled" value="false"/>
<property name="search.use.commit.thread" value="false"/>
<property name="search.lucene.use.ram.directory" value="true"/>
<property name="search.query.wait.for.txns" value="false"/>
<property name="logging.maxLogFileSize" value="100"/>
<property name="logging.maxBackups" value="10"/>
</tc-properties>
<system>
<configuration-model>development</configuration-model>
</system>
<servers>
<server host="localhost" name="Terracotta Server1">
<!-- Specify the path where the server should store its data. -->
<data>C:\Program Files\Terracotta\terracotta-3.7.5\server\server-data</data>
<logs>C:\Program Files\Terracotta\terracotta-3.7.5\server\server-logs</logs>
<index>C:\Program Files\Terracotta\terracotta-3.7.5\server\server-index</index>
<statistics>C:\Program Files\Terracotta\terracotta-3.7.5\server\server-statistics</statistics>
<!-- Specify the port where the server should listen for client
traffic. -->
<dso-port bind="127.0.0.1">9510</dso-port>
<jmx-port bind="127.0.0.1">9520</jmx-port>
<l2-group-port bind="localhost">9530</l2-group-port>
<!--jmx-port bind="xxx.xxx.xxx.xxx">9520</jmx-port>
<tsa-port>9510</tsa-port>
<tsa-group-port>9530</tsa-group-port-->
<!-- Enable BigMemory on the server. -->
<!--offheap>
<enabled>true</enabled>
<maxDataSize>4g</maxDataSize>
</offheap-->
<authentication/>
<dso>
<client-reconnect-window>120</client-reconnect-window>
<persistence>
<mode>permanent-store</mode>
<!--mode>temporary-swap-only</mode-->
<!--<offheap>
<enabled>false</enabled>
<maxDataSize>450m</maxDataSize>
</offheap>-->
</persistence>
<garbage-collection>
<enabled>true</enabled>
<verbose>false</verbose>
<interval>300</interval>
</garbage-collection>
</dso>
</server>
<mirror-groups>
<mirror-group group-name="group1">
<members>
<member>Terracotta Server1</member>
</members>
<ha>
<mode>networked-active-passive</mode>
<networked-active-passive>
<election-time>5</election-time>
</networked-active-passive>
</ha>
</mirror-group>
</mirror-groups>
<ha>
<mode>networked-active-passive</mode>
<networked-active-passive>
<election-time>5</election-time>
</networked-active-passive>
</ha>
<update-check>
<enabled>false</enabled>
</update-check>
<!-- Add the restartable element for Fast Restartability (optional). -->
<!--restartable enabled="true"/-->
</servers>
<clients>
<logs>logs-%i</logs>
</clients>
</tc:tc-config>
Which version of Terracotta libraries are you using ? The tc-config.xml has some updated tags in 4.x version onwards. Please make sure the tc-config and ehcache xml obeys the schema.
Cheers,
Ridhav
I saw you tried to use the mirror-group tag in your tc config, but it seems you misplaced it.
For example, this config should work :
<?xml version="1.0" encoding="UTF-8" ?>
<tc:tc-config xsi:schemaLocation="http://www.terracotta.org/schema/terracotta-8.xsd"
xmlns:tc="http://www.terracotta.org/config"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<tc-properties>
<property name="l2.l1reconnect.enabled" value="true"/>
<property name="l2.l1reconnect.timeout.millis" value="5000"/>
<property name="sigar.enabled" value="false"/>
<property name="search.use.commit.thread" value="false"/>
<property name="search.lucene.use.ram.directory" value="true"/>
<property name="search.query.wait.for.txns" value="false"/>
<property name="logging.maxLogFileSize" value="100"/>
<property name="logging.maxBackups" value="10"/>
</tc-properties>
<servers>
<mirror-group group-name="group1">
<server host="localhost" name="Terracotta Server1">
<!-- Specify the path where the server should store its data. -->
<data>C:\Program Files\Terracotta\terracotta-3.7.5\server\server-data</data>
<logs>C:\Program Files\Terracotta\terracotta-3.7.5\server\server-logs</logs>
<index>C:\Program Files\Terracotta\terracotta-3.7.5\server\server-index</index>
<!--<statistics>C:\Program Files\Terracotta\terracotta-3.7.5\server\server-statistics</statistics>-->
<!-- Specify the port where the server should listen for client
traffic. -->
<tsa-port>9510</tsa-port>
<jmx-port>9520</jmx-port>
<tsa-group-port>9530</tsa-group-port>
<authentication/>
</server>
</mirror-group>
<update-check>
<enabled>false</enabled>
</update-check>
<!-- Add the restartable element for Fast Restartability (optional). -->
<!--restartable enabled="true"/-->
</servers>
<clients>
<logs>logs-%i</logs>
</clients>
</tc:tc-config>
Take a look at this line that defines the schema used to construct your xml configuration file:
<tc:tc-config xsi:schemaLocation="http://www.terracotta.org/schema/terracotta-8.xsd
Enter the address provided and take a look at how nested xml elements and attributes are allowed to be used.
For instance, element dso-port is allowed in terracotta-6.xsd and terracotta-7.xsd, but not in the terracotta-8.xsd, which is the one you declared.
By the way, depending on the version of terracotta server you use, the appropriate xml schema should be declared due to element incompatibilities.