I am building a Java SE application that is powered by Hibernate. Mainly many Java SE instances would run and there would be many Hibernate Session factories. When one client machine insert an Object to the datasource, other clients won't see it unless I clear the cache. Is there a better mechanism to use in this case? Below is my cache clearing method.
public static void clearCache() {
HibernateHelper.beginTranscation();
HibernateHelper.getCurrentSession().clear();
HibernateHelper.getSessionFactory().evictQueries();
try {
Map<String, ClassMetadata> classesMetadata = HibernateHelper
.getSessionFactory().getAllClassMetadata();
for (String entityName : classesMetadata.keySet()) {
HibernateHelper.getSessionFactory().evictEntity(entityName);
}
} catch (Exception e) {
}
HibernateHelper.commit();
}
Please note the fact that I am using the Second Level cache (Memcache) and Query cache as well.