Bonjour,
J'ai mis en place le cache de second niveau pour JPA/Hibernate et j'ai constaté un problème en tracant les requêtes. Lorsque j'effectue plusieurs appels de mon DAO a partir d'une classe de test, le premier appel effectue la recherche en base, le second appel récupère le résultat en cache (d'après les logs) mais à priori un select est généré pour chacun des éléments récupéré du cache. Y-a-t-il une solution pour empêcher ces selects ?
Merci pour votre aide.
Code:
Extrait du DAO
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:
Classe de test :
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:
Extrait 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:
Extrait 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
===> Pb car les requêtes ci-dessous sont exécutées
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=?