Alachisoft.com

Contact Us
Live Chat

Hibernate Second Level Cache

Hibernate is a free and a popular object-relational mapping engine for Java. It simplifies your development by letting you map domain objects to your database tables and not have to write database persistence code in JDBC.

For most high traffic Java applications using Hibernate, scalability is critical. But, they are making heavy use of data that resides in the database and the database quickly becomes a scalability bottleneck. To handle this, Hibernate Caches are provided. Hibernate Caches come in two levels; built-in First Level (L1) Cache at “Session Object” level and a pluggable Second Level (L2) Cache framework at “Session Factory” level (meaning at application level). And, through this Second Level Cache framework, you can plug-in JvCache.

JvCache is an in-memory data grid for Java and lets you cache application data and reduce database trips. As a result, you improve your Java application performance and scalability. And, JvCache also provides a Hibernate Second Level Cache provider that plugs-in without any code changes. Please see details of JvCache.

Why Use JvCache as Hibernate Second Level Cache?

JvCache offers a powerful in memory data grid to those Java applications that are running in a multi-server environment. And, if these applications are already using Hibernate, then JvCache becomes an ideal Hibernate Second Level Cache provider for them. Here are a few reasons why you should use JvCache as Hibernate Second Level Cache:

  • Hibernate Cache consistency in multi-server environment: JvCache runs in a multi-server environment and keeps the Hibernate cache consistent across these servers whenever you make any updates to the cache.
  • Scale Hibernate Cache size: JvCache allows you to add more cache servers at runtime to grow storage capacity because it pools their memory together into one logical cache storage.
  • Linear scalability for extreme transactions: As you increase transaction load on your Java applications, your database is unable to scale and it becomes a bottleneck. But, JvCache allows you to scale your transaction capacity by simply adding more cache servers and scales linearly.
  • Data replication provides reliability of Hibernate Cache: JvCache has a rich set of caching topologies with intelligent replication for data reliability but one that does not slow down the Hibernate Cache. As a result, you can rest assured that your data won’t be lost even if a cache server goes down.

No Programming for using JvCache as Hibernate Second Level Cache

Hibernate has a pluggable Second Level Cache provider architecture and JvCache has implemented this cache provider. As a result, you can incorporate JvCache into your Java application that is using Hibernate Cache without making any code changes. You only need to modify app.config as follows:

  • Set hibernate.cache.provier_class to com.alachisoft.jvcache.integrations.hibernate.cache.JvCacheProvider Cache property under Optional Properties -> Cache Properties in your hibernage.cfg.xml file.
  • Add jvcache.xml file under default package in your hibernate application.
  • JvCache.xml file has its contents as follows:
    <configuration>
    	   <jvcache>
    		       <region>
    			           .
    			           .
    			           .
    		       </region>
    	   </jvcache>
    </configuration>

  • Define cache region as follows:
    <region name=”default”>
    	         <add key=”cacheName” value=”mycache” />
    	         <add key=”enableCacheException” value=”false” />
    	//where “mycache” is the name of cache you want to use with your hibernate application.
    </region>

  • Refer jvcache.xml in hibernate.cfg.xml file as follows:
    <property name="hibernate.cache.provider_configuration_file_resource_path">
    ../Hibernate/src/jvcache.xml </property>

    Replace ‘../Hibernate/src/jvcache.xml’ with the actual jvcache.xml path on your machine.

JvCache Features for Hibernate Second Level Cache

With JvCache plugged as Hibernate Second Level Cache, you suddenly get access to an enterprise level in-memory data grid for your Java application. Here are some features that JvCache integration for Hibernate Cache provides over and above regular Second Level Cache:

  1. Database synchronization of Hibernate Cache: Most data that you cache typically comes from a relational database like Oracle, SQL Server, DB2, and others. JvCache allows you to synchronize any database changes with the in-memory data grid so it stays fresh and consistent. JvCache provides OracleDependency for Oracle and DbDependency for other databases. You can enable this feature with Hibernate through config file changes.
  2. Async operations: If you do not want your application to wait for the cache to be updated, you can use the async version of the cache operations. In this case, the control immediately returns to your application and the cache is updated in the background. You can enable this feature with Hibernate as well.
  3. Absolute Expiration: JvCache allows you to specify a date-time value when a cached item should be expired. You can specify an “interval” in Hibernate config files and then JvCache uses “Now() + interval” formula to calculate the date-time value for absolute expiration.
  4. Sliding Expiration: JvCache allows you to specify an “interval” value indicating that this cached item should be expired if untouched for this long. You can specify this interval through jvcache.xml for Hibernate.
  5. Cache item priority: You can specify a priority for each cached item based on its class name. Then, JvCache uses this information to determine whether to evict this item or not. JvCache provides priority based evictions where cached items are evicted based on their priority. You can enable this feature through jvcache.xml changes for Hibernate.
  6. Compact Serialization: As you know, serialization is a very costly operation in caching. And, using Java Reflection for it is extremely slow while writing custom serialization code is time consuming and full of human errors. JvCache has a dynamic compact serialization feature where JvCache generates custom serialization code, compiles it in-memory, and uses it for serialization/deserialization of objects if you wish. You can use this for some objects and not the others. This is approximately 10 times faster than Java Reflection based serialization. And, you can use this feature with Hibernate. Just register your Hibernate Entity classes with JvCache.

Directly Use JvCache from Hibernate Apps

Despite the fact that Hibernate Second Level Cache provider makes it really easy for you to incorporate JvCache, you should keep in mind that you only get a subset of JvCache features through this integration.

If you want to benefit from all of JvCache features, then you should make direct API calls to it. Although, there is a small programming effort involved, you would most likely end up with greater benefits than using Hibernate Second Level Cache provider path.

JvCache Features Missed in Hibernate Second Level Cache

Below is a list of JvCache features that are not provided with Hibernate Second Level Cache provider integration:

  1. Bulk operations
  2. Groups and subgroups
  3. Tags & Named Tags
  4. Object Query Language (OQL) based searches
  5. Cache Dependencies
    1. Key based
    2. File based
    3. Custom
  6. Lock and Unlock of cached items
  7. Cache item versioning
  8. Streaming API
  9. Read-through, Write-through, and Write-behind
  10. Event Notifications
    1. Item based (update and remove)
    2. General purpose (add, update, remove)
    3. Custom
  11. Continuous Query
  12. Runtime Data Sharing (Java-to-Java and Java-to-.NET)
  13. Cache Loader

What to Do Next?