-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: JPA 2.0 2nd level cache
PostPosted: Wed Mar 31, 2010 4:27 pm 
Newbie

Joined: Wed Mar 31, 2010 4:15 pm
Posts: 2
I'm trying to figure out how I can get the standardized JPA 2.0 2nd level cache to work. The code is working when I use the @org.hibernate.annotations.Cache annotation on my entity so ehcache configuration etc. is correct. Without this annotation the cache doesn't work at all.

My configuration is as follows:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">

    <persistence-unit name="testPU"  transaction-type="RESOURCE_LOCAL">
       
        <properties>
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/jpa_course"/>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
            <property name="javax.persistence.jdbc.user" value="root"/>
            <property name="javax.persistence.jdbc.password" value=""/>
            <property name="javax.persistence.sharedCache.mode" value="ALL"/>

            <property name="hibernate.archive.autodetection" value="class"/>
            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.format_sql" value="true"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
            <property name="hibernate.hbm2ddl.auto" value="update"/>


            <property name="hibernate.cache.use_second_level_cache" value="true"/>
            <property name="hibernate.cache.use_query_cache" value="false"/>
            <property name="hibernate.cache.provider_class" value="org.hibernate.cache.EhCacheProvider"/>

        </properties>
    </persistence-unit>


My entity is annotated as follows (all tough that should be true on default anyway):
Code:
@Entity
@Cacheable(true)
public class Employee {


My test code creates a new entitymanager each time and uses a EntityManager.find to load the entity.
Code:

        for (int i = 0; i < 10000; i++) {
            em = emf.createEntityManager();

            em.find(Employee.class, 1L);
            em.close();
        }


There is an Employee with id 1 in the database.

Each find will execute another select query however, so the cache is not working at all. If I add @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) to the entity, it works correctly. That should not be necessary in JPA 2 however.


Top
 Profile  
 
 Post subject: Re: JPA 2.0 2nd level cache
PostPosted: Wed Mar 31, 2010 5:10 pm 
Newbie

Joined: Wed Mar 31, 2010 4:15 pm
Posts: 2
I found the following Jira issue which might be related: http://opensource.atlassian.com/project ... e/HHH-4835

Can anyone confirm this is the reason why the cache doesn't work as expected?


Top
 Profile  
 
 Post subject: Re: JPA 2.0 2nd level cache
PostPosted: Sat Nov 26, 2011 6:48 pm 
Beginner
Beginner

Joined: Fri Jan 23, 2009 10:34 am
Posts: 25
Location: Switzerland
The post is a bit old but anyway...

Under JBoss AS7 with default JPA2 provider (Hibernate 4), I found that having the property
Code:
<property name="javax.persistence.sharedCache.mode" value="ALL"/>

has no effect on the cache. In order to make the L2 cache work, I added the following tag in the <persistence-unit> tag of the persistence.xml:
Code:
<shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>

And in the properties (an exception is raised if not set):
Code:
<property name="hibernate.cache.use_second_level_cache" value="true"/>


The shared-cache-mode which should be defaulted to "ENABLE_SELECTIVE" seems not so default (at least under JBoss AS7), since I had to set it explicitely.

The javax.persistence.sharedCache.mode is said to correspond to the "shared-cache-mode" tag, but this seems not to be the case. But the documentation states that "All the properties defined in Section 2.2.1, “Packaging” can be passed to the createEntityManagerFactory method and there are a few additional ones [including] javax.persistence.sharedCache.mode", so it is not clear if the "javax.persistence.sharedCache.mode" property is also valid when used in the persistence.xml.

Regards,
Julien


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.