hey,
Some data used by my application are modified by external batchs, so i need to have the possibility to clear my entity/collection cache. I do this using the 2 following methods :
Code:
private void clearCollectionCache(SessionFactory factory) {
Map<String, CollectionMetadata> roleMap = factory.getAllCollectionMetadata();
for (String roleName : roleMap.keySet()) {
factory.getCache().evictCollectionRegion(roleName);
}
}
private void clearEntityCache(SessionFactory factory) {
Map<String, ClassMetadata> entityMap = factory.getAllClassMetadata();
for (String entityName : entityMap.keySet()) {
factory.getCache().evictEntityRegion(entityName);
}
}
Everything works fine. However i also need to clear the query cache for 2 reasons :
- i'm not sure of the behavior of the query cache, the object references of the query cache can no longer be coherent
- i do need to execute my HQL/criteria requests because they performs joins which improve my performance
So I tried to clear the query cache. But when i do so, the request is executed the first time and all the objects i need are retrieved, but the second time, the request doesn't appear (it has been put in cache) but my objects are not retrieved (they need to be retrieved from the db :/ generating a lot of loads...).
Why my objects are not put in the second level cache the first time my request has been performed ?
The third time the objects are correctly retrieved from the cache.
Extra question : when a HQL/ criteria request is executed (not in cache) and retrieve object which are in L2 cache, is the L2 cache used ?
Thanks a lot!