Alachisoft.com

Hibernate Second Level Cache

Integrating TayzGrid into Hibernate solves database scalability limitations for high traffic applications. Your applications benefit from high availability, high scalability and zero data loss. These benefits are essential when using Hibernate in a clustered environment. Furthermore, TayzGrid can be easily plugged into Hibernate using config changes without any code change.

Introduction to Hibernate

Hibernate is one of the most famous Object Relational Mapping library for Java. Having its own native API, and being JPA compliant (Java Persistence API compliant), it supports almost every type of database. It auto-generates classes by mapping domain objects to your database tables, rows and columns using normal Object Oriented idioms such as interfaces, inheritance, Java collections etc. Hibernate also translates Java domain object query syntax into SQL queries. It makes development easier and faster and is built to support high transactions. However, applications soon face scalability bottlenecks in the database tier. To take load off the database, Hibernate provides a sophisticated two level caching mechanism to cache frequently used application data.

Hibernate First Level Caching

The Hibernate First Level Cache resides in-memory, inside your application, and lives within the session scope. Its primary function is to reduce the number of redundant SQL queries within a transaction thus avoiding unnecessary database trips. Itís enabled by default and you cannot disable it. Although this helps improve the overall read performance, the problem still remains: databases cannot scale well at higher transactional load.

Hibernate Second Level Caching

The Hibernate Second Level Cache is the fallback if the requested objects are not found in the first level cache. The Second Level Cache can be configured for every class and collection which you intend to store in the database, giving you complete data control. Second Level Caching is mainly used to store objects across sessions, as it lives within the Session Factory scope, so cached objects are available to the entire application. In a clustered environment, cached data needs to be available out-of-application so that other instances of applications, running on other servers, can utilize the same data thereby reducing the amount of database trips.

TayzGrid with Hibernate - Basic Benefits

Both TayzGrid and Hibernate Second Level Cache live outside your application scope, therefore the TayzGrid/Hibernate cache outlives the application lifetime. This allows you to keep the cache live even if your application process restarts. Additionally, with TayzGrid you can have multiple application processes in a multi-server web farm share a common second level cache. Using the TayzGrid IMDG your N-Tier architecture will now boast high availability in the case of any server failure. It will boast high performance throughput since the data is distributed, thereby reducing the amount of database traffic by up to 80%.

Use Cases of Hibernate with TayzGrid

  • WAN Replication of In-Memory Data Grid: TayzGrid allows you to replicate web sessions across the WAN if your application is running in multiple data centers. You can achieve this for active-passive and active-active datacenter configuration. This way, if one datacenter goes down, you can redirect all your traffic to the other datacenter and all user sessions stay active and valid.
  • Database Synchronization: Most data that you put in data grid typically comes from a relational database like Oracle, SQL Server, DB2, and others. TayzGrid allows you to synchronize any database changes with the In-Memory Data Grid so it stays consistent. TayzGrid provides OracleDependency for Oracle and DbDependency for other databases. You can enable this feature with Hibernate Cache through config file changes.
  • Fast Compact Serialization of Objects: Any objects put in an out-of-process In-Memory Data Grid must be serialized and regular Java and .NET serialization is used for it which is very costly. TayzGrid provides almost 10 times faster serialization called Fast Compact Serialization. And, the best thing is that you don’t have to write any code for it because TayzGrid generates serialization code at runtime, compiles it in-memory, and uses it for all serialization. You can mix Fast Compact Serialziation with regular serialization.

Live Webinars Hibernate Second Level Cache Documentation

Hibernate Second Level Cache Configuration

You can plug-in TayzGrid as Hibernate Second Level Cache without any programming or even recompilation of your code. You only need to modify app.config file as follows.


<hibernate-configuration>
  <session-factory>
  ...
    <property name="hibernate.cache.use_second_level_cache">
      true
    </property>
    <property name="hibernate.cache.region.factory_class">
      com.alachisoft.tayzgrid.integrations.hibernate.cache.TayzGridRegionFactory
    </property>
    <property name="tayzgrid.application_id">
      myapp
    </property>
    <property name="hibernate.cache.use_query_cache">
      true
    </property>
    ...
    </session-factory>
</hibernate-configuration>

<!--2nd section -->

<hibernate-mapping>
  <class name="hibernator.BLL.Customer"
         table="customers">
    <cache usage="read-write"
           region="AbsoluteExpirationRegion"/>
    ...
  </class>
 </hibernate-mapping>

And, here in Customer.hbm.xml, you specify if Hibernate Second Level Cache should store this class in TayzGrid. You do this for each class that Hibernate is using.


<hibernate-mapping>
   <class name="hibernator.BLL.Customer"
          table="customers">
		<cache usage="read-write"
               region="AbsoluteExpirationRegion"/>
  ...
  </class>
</hibernate-mapping>

And, below in TayzGridHibernate.xml you can specify TayzGrid configuration for Hibernate.


<configuration>
  <application-config application-id="myapp"
                      enable-cache-exception="true"
                      default-region-name="DefaultRegion"
                      key-case-sensitivity="false">
    <cache-regions>
    ...
      <region name="DefaultRegion"
              cache-name="mycache"
              priority="default"
              expiration-type="none"
              expiration-period="0"/>
    </cache-regions>
    <database-dependencies>
      <dependency entity-name="hibernator.BLL.Customer"
                  type="oledb"
                  sql-statement="select ContactName from dbo.Customers
                                 where CustomerID =?"
                  cache-key-format="depdency.customer:[pk]"/>
      </database-dependencies>
  </application-config>
</configuration>

Directly Using TayzGrid API from Hibernate Apps

Despite the fact that Hibernate Second Level Cache provider makes it really easy for you to incorporate TayzGrid into your Java application, you should keep in mind that you only get a subset of TayzGrid features through this integration. If you make direct API calls into TayzGrid through a small programming effort, you would gain the following features.

  1. Runtime data sharing (events, Continuous Query, etc.)
  2. Handling data relationships
  3. Bulk operations
  4. Groups, Tags & Named Tags
  5. SQL-like searching
  6. Lock and Unlock of data grid items
  7. Versioning of items in data grid
  8. Streaming API
  9. Read-through, Write-through, and Write-behind