Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not able to configure JPA with ehcache

Tags:

java

jpa

ehcache

I have been trying to configure JPA with ehcache but no success till now. The configurations which i am doing are :

  • persistence.xml

    <persistence-unit name="customDatabase">
      <jta-data-source>jdbc/oracleXE_DS</jta-data-source>
      <class>com.td.waw.cse.entities.Product</class>
      <properties>
                       <property name="openjpa.Log" value="DefaultLevel=TRACE, Runtime=INFO, Tool=INFO, SQL=TRACE"/>
               <property name="openjpa.QueryCache" value="net.sf.ehcache.openjpa.datacache.EhCacheQueryCache"/>
       <property name="openjpa.DataCacheManager" value="net.sf.ehcache.openjpa.datacache.EhCacheDataCacheManager"/>
       <property name="openjpa.DataCache" value="net.sf.ehcache.openjpa.datacache.EhCacheDataCache"/>
       <property name="openjpa.RemoteCommitProvider" value="net.sf.ehcache.openjpa.datacache.NoOpRemoteCommitProvider"/>
    </properties>
    
  • ehcache.xml

    <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:noNamespaceSchemaLocation="ehcache.xsd"
             updateCheck="true" monitoring="autodetect"
             dynamicConfig="true" >
        <defaultCache
        maxElementsInMemory="1000"
        eternal="false"
        timeToIdleSeconds="300"
        timeToLiveSeconds="600"
        overflowToDisk="false"
        memoryStoreEvictionPolicy="LRU"
        />
    
        <!-- OpenJPA data cache -->
        <cache name="openjpa"
        maxElementsInMemory="5000"
        eternal="false"
        timeToIdleSeconds="300"
        timeToLiveSeconds="600"
        overflowToDisk="false"
        memoryStoreEvictionPolicy="LRU"
        />
    
        <!-- OpenJPA query cache -->
        <cache name="openjpa-querycache"
        maxElementsInMemory="1000"
        eternal="false"
        timeToIdleSeconds="300"
        timeToLiveSeconds="600"
        overflowToDisk="false"
        memoryStoreEvictionPolicy="LRU"
        />
    </ehcache>
    
  • Product.java

    @Entity
    @Table(name="PRODUCT")
    @NamedQueries({@NamedQuery(name="getAllProducts", query = "select products from Product products")})
    public class Product implements Serializable {}
    

I am not getting any exception but i could not see the ehcache working as nothing specific to ehcache printed in the logs. I would really appreciate if someone can help in this.

like image 291
Gagan Arora Avatar asked Oct 13 '22 17:10

Gagan Arora


1 Answers

Add the following properties in persistence.xml:

<property name="openjpa.QueryCache" value="ehcache"/>
<property name="openjpa.DataCacheManager" value="ehcache"/>

Add the following jars in classpath:ehcache-core-2.4.4.jar, ehcache-openjpa-0.2.0.jar and slf4j-api-1.6.1.jar.

That's all.

Jar Downloads:

  • link - http://ehcache.org/downloads/catalog?activated=true
  • ehcache-core-2.4.4.jar and slf4j-api-1.6.1.jar - ehcache-core-2.4.4-distribution.tar.gz module
  • ehcache-openjpa-0.2.0.jar - ehcache-openjpa-0.2.0-distribution.tar.gz

References

  • L2 caching explained - http://blogs.oracle.com/carolmcdonald/entry/jpa_caching
  • OpenJPA L2 caching implementation - http://openjpa.apache.org/builds/1.0.2/apache-openjpa-1.0.2/docs/manual/ref_guide_caching.html
  • Ehcache OpenJPA implementation - http://www.ehcache.org/documentation/user-guide/openjpa-provider
like image 71
Sanjeev Kumar Dangi Avatar answered Nov 09 '22 14:11

Sanjeev Kumar Dangi