In Hibernate, we can use different cache providers for implementing
second level cache at JVM/SessionFactory level.
Some of these are:
Hashtable
EHCache
OSCache
SwarmCache
JBoss Cache 1.x
JBoss Cache 2
xxxxxxxxxx
public class HibernateEHCacheMain {
public static void main(String[] args) {
System.out.println("Temp Dir:"+System.getProperty("java.io.tmpdir"));
//Initialize Sessions
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Statistics stats = sessionFactory.getStatistics();
System.out.println("Stats enabled="+stats.isStatisticsEnabled());
stats.setStatisticsEnabled(true);
System.out.println("Stats enabled="+stats.isStatisticsEnabled());
Session session = sessionFactory.openSession();
Session otherSession = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
Transaction otherTransaction = otherSession.beginTransaction();
printStats(stats, 0);
Employee emp = (Employee) session.load(Employee.class, 1L);
printData(emp, stats, 1);
emp = (Employee) session.load(Employee.class, 1L);
printData(emp, stats, 2);
//clear first level cache, so that second level cache is used
session.evict(emp);
emp = (Employee) session.load(Employee.class, 1L);
printData(emp, stats, 3);
emp = (Employee) session.load(Employee.class, 3L);
printData(emp, stats, 4);
emp = (Employee) otherSession.load(Employee.class, 1L);
printData(emp, stats, 5);
//Release resources
transaction.commit();
otherTransaction.commit();
sessionFactory.close();
}
private static void printStats(Statistics stats, int i) {
System.out.println("***** " + i + " *****");
System.out.println("Fetch Count="
+ stats.getEntityFetchCount());
System.out.println("Second Level Hit Count="
+ stats.getSecondLevelCacheHitCount());
System.out
.println("Second Level Miss Count="
+ stats
.getSecondLevelCacheMissCount());
System.out.println("Second Level Put Count="
+ stats.getSecondLevelCachePutCount());
}
private static void printData(Employee emp, Statistics stats, int count) {
System.out.println(count+":: Name="+emp.getName()+", Zipcode="+emp.getAddress().getZipcode());
printStats(stats, count);
}
}
https://www.digitalocean.com/community/tutorials/hibernate-ehcache-hibernate-second-level-cache
xxxxxxxxxx
Caching is a mechanism to enhance the performance of a system.
It is a buffer memory that lies between the application and the database.
Cache memory stores recently used data items in order to reduce the number of database hits as much as possible.
Hibernate provides 3 types of caching.
Session Cache - The session cache caches objects within the current session.
It is enabled by default in Hibernate.
Objects in the session cache reside in the same memory location.
Second Level Cache - The second level cache is responsible for caching objects across sessions.
When this is turned on, objects will first be searched in the cache and if they are not found, a database query will be fired.
The second-level cache will be used when the objects are loaded using their primary key.
This includes fetching of associations.
Second-level cache objects are constructed and reside in different memory locations. T
he second-level cache is required to be configured with external cache provider like EhCache.
Query Cache - Query Cache is used to cache the results of a query.
When the query cache is turned on, the results of the query are stored against the combination query and parameters.
Every time the query is fired the cache manager checks for the combination of parameters and query.
If the results are found in the cache, they are returned, otherwise, a database transaction is initiated.