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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
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.
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<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.
I simply want to tell you that I am just beginner to blogs and absolutely enjoyed you’re website. More than likely I’m planning to bookmark your blog . You really come with awesome stories.
Given so much info in it, These type of articles keeps the users interest in the website, and keep on sharing more .
java training in chennai
useful blog … thanks for sharing