Step 3: Enable DBCacheDependency in NHibernate
To enable Database Cache Dependency in nhibernate items, you need to define <database-dependencies> 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 <database-dependencies> section as follows:
<configuration>
<application-config application-id="myapp" enable-cache-exception="true" default-region-name="default"
key-case-sensitivity="false">
<cache-regions>
<region name="AbsoluteExpirationRegion" cache-name="myCache" priority="Default" expiration-type="sliding"
expiration-period="180"/>
<region name="default" cache-name="myCache" priority="default" expiration-type="none"
expiration-period="0" />
</cache-regions>
<database-dependencies>
<dependency entity-name="nhibernator.BLL.Customer" type="oledb" sql-statement="select ContactName from
dbo.Customers where CustomerID =?" cache-key-format="depdency.customer:[pk]"/>
</database-dependencies>
</application-config>
</configuration>
The type of database dependency is either "sql", "oracle", "oledb". sql is a mechanism for synchronizing your cache with SQL Server 2005 database through .NET events. oledb allows you to synchronize your cache with any OLEDB compliant database through a configurable polling interval. If you specify "sql" value, then you must also specify the sql-statement="...". 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