Alachisoft NCache 4.1 - Online Documentation

Use DB Dependency with Hibernate

 
To enable Database Cache Dependency in Hibernate items, you need to define <ncache> configuration section in the application's configuration file (it could be app.config file if it is a desktop application or web.config file if it is a web application) and enable database notifications on the database.
To enable DBCacheDependency in Hibernate, do the following:
 
  • Include the following import statements in your application:
     
import com.alachisoft.ncache.runtime.dependencies.*;
import com.alachisoft.ncache.web.caching.*;
 
  • Provide dbSync type in <ncache> section as follows:
     
    <ncache>
    <dbSync type = "sqlDependency"/>
    <class name = "hibernator.BLL.Customer">
    <CacheKey format = "Customer:CustomerID:[pk]"/>
    <sqlDependency sql = "select ContactName from dbo.Customers where CustomerID =?"/>
    </class>
    <class name = "hibernator.BLL.Order">
    <CacheKey format = " [pk]:Order"/>
    <sqlDependency sql = "select ContactName from dbo.Orders where OrderID=?"/>
    </class>
    </ncache>
     
    <ncache> section contains following configurable options to enable Database Cache Dependency:
     
  • dbsync: The value for dbSync type is either "sqlDependency" or "dbDependency". sqlDependency is a mechanism for synchronizing your cache with SQL Server 2005 database through java events. dbDependency allows you to synchronize your cache with any OLEDB compliant database through a configurable polling interval.
 
  • CacheKey: You can specify the cache-key format here. The example above shows "Customer:CustomerID:[pk]" which would translate into "Customer:CustomerID:1000" if the CustomerID value of the object is 1000.
 
  • sqlDependency: If you specify "sqlDependency" value, then you must also specify the <sqlDependency=""/> tag for individual classes in <class> section. You specify a "select" SQL statement to identify a row in SQL Server 2005 database that corresponds to this cached item. Then, whenever that row is updated or removed, this cached item is also removed. The "?" in the command will be replaced with the primary key of the mapped table at runtime.
     
  • Enable database notifications on the database except when you are using SQL Server 2005. For further details see Enabling Database Notifications for SQL Server, Enabling Database Notifications for Oracle and Enabling Database Notifications for DB2.
 
 
See Also
 
Copyright © 2005-2012 Alachisoft. All rights reserved.