Does EntityManager Flush and Clear methods empty ehcache?


#1

Hello, I am working on legacy hibernate application and noticed that post querying the selects of huge data, the application code is flushing and clearing secondary cache. Now, the subsequent request is again taking the same time as first request.

Let me know if flush and clear of EM also clears secondary cache?

Thanks.


#2

If you’re clearing the EntityManager, then only the Session or the first level cache is cleared.

You need to add the code that your application is executing to understand what you are doing.


#3

Thank you Vlad. Here is the code.

CriteriaBuilder builder = entityManager.getCriteriaBuilder();
CriteriaQuery<Tuple> criteria = builder.createTupleQuery();
...
TypedQuery<Tuple> tupleQuery = entityManager.createQuery(criteria);
tupleQuery.setHint(QueryHints.HINT_READONLY, true);
tupleQuery.setHint(QueryHints.HINT_FETCH_SIZE,1000);	
tupleQuery.setFirstResult(0);
tupleQuery.setMaxResults(10000);	
tupleQuery.setHint(QueryHints.HINT_CACHEABLE,true);	
List<Tuple> resultList = tupleQuery.getResultList();
if (entityManager != null) {
	entityManager.flush();
	entityManager.clear();
}

#4

That does only clear the first level cache. You should enable Hibernate statistics as well as the cache region logging to see how effective the cache is.