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:
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:
-
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.
-
See Also