Step 3: Enable DBCacheDependency in NHibernate
To enable Database Cache Dependency in nhibernate items, you need to define <ncache> configuration section in the application's configuration file (it could be app.config file if its a desktop application or web.config file if its a web application) and enable database notifications in the database.
To enable DBCacheDependency in NHibernate, do the following:
-
Provide dbSync type in <ncache> section as follows:
<ncache>
<region name = "default">
<add key = "cacheName" value = "mycache"/>
<add key = "enableCacheException " value = "false"/>
<class name = "nhibernator.BLL.Customer">
<cacheKey format = "[pk]:Customer:CustomerID"/>
<sqlDependency sql = "select ContactName from dbo.Customers where CustomerID =?"/>
</class>
</region>
<region name = "Order">
<add key = "cacheName" value = "CC"/>
<add key = "enableCacheException" value = "false"/>
<class name = "nhibernator.BLL.Product">
<cacheKey format = " [pk]:Product:ProductID"/>
<sqlDependency sql = " select ProductName from dbo.Product where ProductID =?"/>
</class>
</region>
</ncache>
The value for dbSync type is either "sqlDependency" or "dbDependency". sqlDependency is a mechanism for synchronizing your cache with SQL Server 2005 database through .NET events. dbDependency allows you to synchronize your cache with any OLEDB compliant database through a configurable polling interval. If you specify "sqlDependency" value, then you must also specify the <sqlDependency sql="..."/> 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.
-
See Also