Database Dependency Using Oracle
NCache provides OracleCacheDependency
for notification based dependencies with
Oracle. Internally, NCache uses OracleDependency
to register data change
notifications with the Oracle database server. Hence you need to understand the
limitations and working mechanisms of OracleDependency
while using this
dependency. For example, limited sets of queries can be registered with
OracleDependency
. See Special Considerations Using Query
Notifications and
OracleDependency
class for more detail.
Oracle Dependency is provided by NCache for synchronizing cache with Oracle database. Item expires if result of the command (based on command text) changes. Oracle Dependency is only available for Oracle database 10g release 2 or later. Also make sure that Oracle Data Providers for .NET (version 10.1.0.2.0 or later) is installed.
Configuring Notification on Oracle Server
NCache tracks changes in a database using notifications received from Oracle server about changes in the database. For this purpose, NCache registers CHANGE NOTIFICATION with the Oracle server. NCache server should have the CHANGE NOTIFICATION privileges to create a notification registration. If NCache server machine does not have these privileges, the following statement can be used to grant the CHANGE NOTIFICATION privilege to the user.
Adding Data with Oracle Dependency
The following code shows how to add data with OracleCacheDependency
. Altering
data after addition with Oracle Dependency will remove it from cache.
To utilize the API, include the following namespace in your application:
Alachisoft.NCache.Runtime.Dependencies.
//Create object to be added with dependency
Product product = new Product();
product.ProductID = 1001;
product.ProductName = "Chai";
string key = "Product:" + product.ProductID;
try{
//Creating OracleCacheDependency
string connectionString = "User Id=scott;Password=tiger;Data Source=oracle";
string query = "SELECT rowID, productID FROM dbo.Products WHERE productID = 1001";
OracleCacheDependency oracleDependency = new OracleCacheDependency(connectionString, query);
//Adding cache item "Product:1001" with SqlDependency
cache.Insert(key, product, oracleDependency, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.Normal);
//Modify Product record in database while program waits...
Thread.Sleep(5000);
//... and then check for its existence
Object item = cache.Get(key);
if (item == null){
// item removed successfully
}
else{
// item not removed successfully
}
}
catch (OperationFailedException e){ }
In Oracle 10g, database change notifications are only object based. This means
that change notifications will be fired if any row is modified in an object.
Therefore, it is recommended to check rowID to confirm if the altered row is the
one for which the event was registered. RowIDs cannot be retrieved unless
explicitly included in query. So the user has to specifically include rowID in
the query that is being registered with OracleDependency
, otherwise the change
notification will be fired if any row is modified in table.
When rowID is included in a query such as – Select rowID, productID,
productname, unitprice from Products where ProductID = 220
- NCache will save
the rowIDs of rows for which the change notification is registered. When it
receives any change notification, NCache will compare the rowIDs to determine
whether the row changed is the one for which the rowID is registered. Otherwise
NCache will have no way to check against this and items for which change
notification is registered may be removed if any row in the table changes.
In Oracle 11g, both object based and query based (default) notifications are provided. In query based notifications, change of the modified row will be notified only if the change notification is registered for it.