Using NCache as Hibernate Second Level Java Cache

Hibernate is an Object-Relational Mapping library for Java language. It provides you mapping from Java classes to database table and reduces the overall development cycle. Because of benefits Hibernate provides, more and more high transactional applications are developed using Hibernate. Here is an example of Hibernate in a Java application.


import org.hibernate.*;

public class HibernateSample
{
	...
Session session = factory.openSession();
session = factory.openSession();
Transaction tx = session.beginTransaction();
Query query =  session.createQuery("from Customer c");
Iterator it = query.list().iterator();
while (it.hasNext ()){
Customer customer = (Customer) it.next();
...
}
       tx.commit();
session.close();
}

But, these high traffic Hibernate applications are encountering a major scalability issue. Although they are able to scale at application-tier level, their database or data storage is not able to scale with the growing number of transactional load.

Java distributed caching is the best technique to solve this problem because it reduces the expensive trips to database that is causing scalability bottlenecks. For this reason, Hibernate provides a caching infrastructure that includes first-level and second-level cache.

Hibernate first-level cache provides you a basic standalone (in-proc) cache which is associated with the Session object, and is limited to the current session only. But, the problem with Hibernate first-level cache is that it does not allow the sharing of object between different sessions. If the same object is required by different sessions all of them make the database trip to load it which eventually increases database traffic and causes scalability problem. Moreover, when the session is closed all the cache data is also lost and next time you have to fetch it again from database.

These high traffic Hibernate applications with only first-level when deployed in the web farm also faces across server cache synchronization problem. In web farm, each node runs a web server – Apache, Oracle WebLogic etc. – with multiple instances of httpd process to server the requests. And, Hibernate first-level cache in each httpd worker process has a different version of the same data cached directly from the database.

Download NCache free trial - Extremely fast and scalable in-memory distributed cache

That is why Hibernate provides you a second-level cache with provider model. Hibernate second-level cache allows you to plug-in 3rd party distributed (out-proc) caching provider to cache object across sessions and servers. Hibernate second-level cache is associated with SessionFactory object and is available to entire application, instead of single session.

When you enable Hibernate second-level cache, you end up with two caches; one is first-level cache and the other is second level cache. Hibernate always tries to retrieve the objects from first-level cache if fails tries to retrieve them from second-level cache. If that also fails then objects are directly loaded from the database and cached as well. This configuration significantly reduces the database traffic because most of the data is served by the second-level distributed cache.

NCache Java has implemented a Hibernate second-level caching provider by extending org.hibernate.cache.CacheProvider. You can easily plug in NCache Java Hibernate distributed caching provider with your Hibernate application without any code changes. NCache allows you to scale your Hibernate application to multi-server configurations without database becoming the bottleneck. NCache also provides you all the enterprise level distributed caching features like data size management, data synchronization across server and database etc.

You can plug in NCache Java Hibernate caching provider by simply modifying your hibernate.cfg.xml and ncache.xml as follows

<hibernate-configuration>
  <session-factory>
<property name = "cache.provider_class">
alachisoft.ncache.integrations.hibernate.cache.NCacheProvider,
alachisoft.ncache.integrations.hibernate.cache
</property>
  </session-factory>
</hibernate-configuration>

<ncache>
<region name = "default">
   <add key = "cacheName" value = "myClusterCache"/>
   <add key = "enableCacheException" value = "false"/>
   <class name = "hibernator.BLL.Customer">
<add key = "priority" value = "1"/>
<add key = "useAsync" value = "false"/>
<add key = "relativeExpiration" value = "180"/>
   </class>
</region>
</ncache>

Hence, by using NCache Java Hibernate distributed cache provider you can linearly scale your Hibernate applications without any code changes.

So, download a fully working 60-day trial of NCache Enterprise and try it out for yourself.

Download NCache Trial | NCache Details

This entry was posted in Hibernate Second Level Cache, Java Distributed Cache and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *


6 − one =

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>