-->
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.  [ 1 post ] 
Author Message
 Post subject: Second-level cache problem with Hibernate/JPA
PostPosted: Thu May 14, 2009 4:38 am 
Newbie

Joined: Thu Dec 29, 2005 6:14 am
Posts: 2
Location: France - Aquitaine
Hello,

We use second-level cache JPA/Hibernate in our application but it seems that we have problems. When we call the DAO many times from a test class, the first call retrieve data from DB, the second call retrieve data from the cache (see the logs extract) but hibernate execute other select requests to fetch data from the database. It seems that there is one request per element retrieved from the cache. Is there anyone who can tell me what i am doing wrong ?

Thanks for your help

Code:
DAO method
   public List<User> loadUsers() {
      List<User> result = new ArrayList<User>();
      
      EntityManager manager = factory.createEntityManager();
      try {
         Query query = manager.createQuery("FROM UserDTO");
            query.setHint("org.hibernate.cacheable", Boolean.TRUE);
            query.setHint("org.hibernate.cacheMode", CacheMode.NORMAL);
         List<UserDTO> UserssDTO = query.getResultList();
         for (UserDTO userDTO : UserssDTO) {
            result.add(userDTO.getUser());
         }
      } finally {
         manager.close();
      }
      
      return result;
      
   }


Code:
Test class:
    public static void main(String[] args) throws IOException, UserUnavailableException {
        BasicConfigurator.configure();
        ContextParameters paramContext = new ContextParameters();
        paramContext.load();
        factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT, paramContext);
        UserDAO dao = new UserDAOImpl(factory);
        for(int i = 1; i <= 3; i++) {
            TestTimer timer = new TestTimer("testLoadAlUsersManyTimes " + i);
            List<User> services = dao.loadUsers() ;
            timer.done();
        }
    }



Code:
persistence.xml
        <properties>
            <property name="fr.cvf.mgs.hub.enabler.core.dao.user.UserDTO" value="read-only"/>
            <property name="fr.cvf.mgs.hub.enabler.core.dao.service.ServiceDTO" value="read-only"/>
            <property name="hibernate.cache.use_second_level_cache" value="true"/>
            <property name="hibernate.cache.use_query_cache" value="true" />
            <property name="hibernate.cache.provider_class" value="org.hibernate.cache.EhCacheProvider"/>
        </properties>


Code:
ehcache.xml
    <defaultCache
            maxElementsInMemory="1000"
            eternal="false"
            overflowToDisk="true"
            diskPersistent="false"
            memoryStoreEvictionPolicy="LRU"/>

     <cache name="fr.cvf.mgs.hub.enabler.core.dao.user.UserDTO"
           maxElementsInMemory="600"/>

     <cache name="fr.cvf.mgs.hub.enabler.core.dao.service.ServiceDTO"
           maxElementsInMemory="600"
           timeToLiveSeconds="65"
           diskExpiryThreadIntervalSeconds="65"/>

    <cache name="org.hibernate.cache.StandardQueryCache"
           maxElementsInMemory="1000"
           eternal="false"
           timeToLiveSeconds="65"
           overflowToDisk="true"/>
           
    <cache name="org.hibernate.cache.UpdateTimestampsCache"
           maxElementsInMemory="5000"
           eternal="true"
           overflowToDisk="true"/>


Quote:
2009-05-13 19:03:54,005 debug [org.hibernate.cache.StandardQueryCache] - query results were not found in cache
2009-05-13 19:03:54,005 debug [org.hibernate.cache.StandardQueryCache] - query results were not found in cache
639 [main] debug org.hibernate.cache.StandardQueryCache - query results were not found in cache
Hibernate:
select
userdto0_.id as id2_,
userdto0_.password as password2_,
userdto0_.quotaMo as quotaMo2_,
userdto0_.userName as userName2_,
userdto0_.version as version2_
from
users userdto0_
2009-05-13 19:03:54,053 debug [org.hibernate.cache.StandardQueryCache] - caching query results in region: org.hibernate.cache.StandardQueryCache; timestamp=5088191421480960
2009-05-13 19:03:54,053 debug [org.hibernate.cache.StandardQueryCache] - caching query results in region: org.hibernate.cache.StandardQueryCache; timestamp=5088191421480960
687 [main] debug org.hibernate.cache.StandardQueryCache - caching query results in region: org.hibernate.cache.StandardQueryCache; timestamp=5088191421480960
testLoadAlUsersManyTimes 1 : 314 ms.
2009-05-13 19:03:54,059 debug [org.hibernate.cache.StandardQueryCache] - checking cached query results in region: org.hibernate.cache.StandardQueryCache
2009-05-13 19:03:54,059 debug [org.hibernate.cache.StandardQueryCache] - checking cached query results in region: org.hibernate.cache.StandardQueryCache
693 [main] debug org.hibernate.cache.StandardQueryCache - checking cached query results in region: org.hibernate.cache.StandardQueryCache
2009-05-13 19:03:54,059 debug [org.hibernate.cache.EhCache] - key: sql: select userdto0_.id as id2_, userdto0_.password as password2_, userdto0_.quotaMo as quotaMo2_, userdto0_.userName as userName2_, userdto0_.version as version2_ from users userdto0_; parameters: ; named parameters: {}
2009-05-13 19:03:54,059 debug [org.hibernate.cache.EhCache] - key: sql: select userdto0_.id as id2_, userdto0_.password as password2_, userdto0_.quotaMo as quotaMo2_, userdto0_.userName as userName2_, userdto0_.version as version2_ from users userdto0_; parameters: ; named parameters: {}
693 [main] debug org.hibernate.cache.EhCache - key: sql: select userdto0_.id as id2_, userdto0_.password as password2_, userdto0_.quotaMo as quotaMo2_, userdto0_.userName as userName2_, userdto0_.version as version2_ from users userdto0_; parameters: ; named parameters: {}
2009-05-13 19:03:54,060 debug [net.sf.ehcache.store.MemoryStore] - org.hibernate.cache.StandardQueryCacheCache: org.hibernate.cache.StandardQueryCacheMemoryStore hit for sql: select userdto0_.id as id2_, userdto0_.password as password2_, userdto0_.quotaMo as quotaMo2_, userdto0_.userName as userName2_, userdto0_.version as version2_ from users userdto0_; parameters: ; named parameters: {}
2009-05-13 19:03:54,060 debug [org.hibernate.cache.StandardQueryCache] - Checking query spaces for up-to-dateness: [users]
2009-05-13 19:03:54,060 debug [org.hibernate.cache.StandardQueryCache] - Checking query spaces for up-to-dateness: [users]
694 [main] debug org.hibernate.cache.StandardQueryCache - Checking query spaces for up-to-dateness: [users]
2009-05-13 19:03:54,060 debug [org.hibernate.cache.EhCache] - key: users
2009-05-13 19:03:54,060 debug [org.hibernate.cache.EhCache] - key: users
694 [main] debug org.hibernate.cache.EhCache - key: users
2009-05-13 19:03:54,060 debug [net.sf.ehcache.store.MemoryStore] - org.hibernate.cache.UpdateTimestampsCacheCache: org.hibernate.cache.UpdateTimestampsCacheMemoryStore miss for users
2009-05-13 19:03:54,060 debug [net.sf.ehcache.Cache] - org.hibernate.cache.UpdateTimestampsCache cache - Miss
2009-05-13 19:03:54,060 debug [org.hibernate.cache.EhCache] - Element for users is null
2009-05-13 19:03:54,060 debug [org.hibernate.cache.EhCache] - Element for users is null
694 [main] debug org.hibernate.cache.EhCache - Element for users is null
2009-05-13 19:03:54,060 debug [org.hibernate.cache.StandardQueryCache] - returning cached query results
2009-05-13 19:03:54,060 debug [org.hibernate.cache.StandardQueryCache] - returning cached query results
694 [main] debug org.hibernate.cache.StandardQueryCache - returning cached query results
===> That is the problem : requests below are generated
Hibernate:
select
userdto0_.id as id2_0_,
userdto0_.password as password2_0_,
userdto0_.quotaMo as quotaMo2_0_,
userdto0_.userName as userName2_0_,
userdto0_.version as version2_0_
from
users userdto0_
where
userdto0_.id=?
Hibernate:
select
userdto0_.id as id2_0_,
userdto0_.password as password2_0_,
userdto0_.quotaMo as quotaMo2_0_,
userdto0_.userName as userName2_0_,
userdto0_.version as version2_0_
from
users userdto0_
where
userdto0_.id=?
Hibernate:
select
userdto0_.id as id2_0_,
userdto0_.password as password2_0_,
userdto0_.quotaMo as quotaMo2_0_,
userdto0_.userName as userName2_0_,
userdto0_.version as version2_0_
from
users userdto0_
where
userdto0_.id=?

.......


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

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.