Alachisoft.com

Contact Us

+1 (925) 236-3830 sales@alachisoft.com

support@alachisoft.com

Live Chat

Using TayzGrid as a Hibernate Second Level Cache

Hibernate is a free and popular object-relational mapping engine for Java that simplifies development by letting you map domain objects to your database tables. And, high traffic Java applications using Hibernate face scalability issues related to database just like all other high traffic applications.

To address this scalability issue, Hibernate provides Second Level Cache architecture which lets you plug in an in-memory data grid. TayzGrid is an extremely fast and linearly scalable in-memory data grid that lets you handle extreme transaction loads. TayzGrid has implemented a Hibernate Second Level Cache provider to allow Hibernate applications to benefit from its capabilities.

Additional Benefits of Using TayzGrid as Hibernate Second Level Cache

  • 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 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.

How to Configure TayzGrid as Hibernate Second Level Cache

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 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

What to Do Next?