Cache Data Dependency on Oracle Database
Note
This feature is only available in NCache Enterprise Edition.
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. See the following Oracle Documentation 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 available for Oracle database 11g or later. Also make sure that Oracle Data Providers for .NET (version 10.1.0.2.0 or later) is installed.
The database change notifications are 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.
Note
Before using Oracle Dependency, set up Oracle Database Environment by referring to Setup Oracle Database Environment section in Administrator's Guide.
Pre-Requisites
- Set up database environment before using Oracle Dependency.
- Include the following namespace in your application:
Alachisoft.NCache.Client
Alachisoft.NCache.Runtime.Dependencies
Alachisoft.NCache.Runtime.Exceptions
- Cache must be running.
- Make sure that the data being added is serializable.
- To ensure the operation is fail safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
Adding Data with Oracle Dependency
Altering data after addition with Oracle Dependency will remove it from the cache. OracleCacheDependency is used to specify the dependency criteria and then item is added in the cache using the Add/Insert method.
The following example adds data with OracleCacheDependency
using the Insert method. The Insert
method adds a new item with dependency and if the item already exists in the cache it overwrites its properties. In case of Java, the item is inserted in the cache after setting the setDependency property of the CacheItem to OracleDependency.
try
{
// Creating a connection string to establish connection with the database
// Connection String is in <AppSettings>
string connectionString = ConfigurationManager.AppSettings["connectionstring"];
// Create the query which selects the data on which key is dependent
string query = "SELECT ROWID, ProductID, ProductName, UnitPrice FROM Products WHERE ProductID > :productID";
var param = new OracleCmdParams();
param.Type = (OracleCmdParamsType.Int16);
param.Value = 1020;
param.Direction = OracleParameterDirection.Input;
// Adding the populated parameter to a dictionary
Dictionary<string, OracleCmdParams> oracleParam = new Dictionary<string, OracleCmdParams>();
oracleParam.Add("productID", param);
// Create Oracle Dependency
var oracleDependency = new OracleCacheDependency(connString, query, OracleCommandType.Text, oracleParam);
// Get Product from database against given product ID
Product product = FetchProductFromDB(param.Value);
// Generate a unique cache key for this product
string key = $"Product:{product.ProductID}";
// Create a CacheItem and add Oracle dependency to it
var cacheItem = new CacheItem(product);
cacheItem.Dependency = oracleDependency;
//Add cache item in the cache with Oracle Dependency
cache.Insert(key, cacheItem);
// For successful addition of item with Oracle Dependency
// Update the record in the database and check if key is present
// This can be done by using
// cache.Contains()
// cache.Count
}
catch (OperationFailedException ex)
{
// Exception can occur due to:
// Connection Failures
// Operation Timeout
// Operation performed during state transfer
}
catch (Exception ex)
{
// Any generic exception like ArgumentNullException or ArgumentException
// Exception may occur due to incorrect query format or invalid connection string
}
Recommendation: To ensure the operation is fail safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
Add Data with Oracle Dependency using Stored Procedure
NCache provides you with an ability to provide Oracle Dependency on an item using stored procedure. You can also specify the parameters to be passed along with the stored procedure using the OracleCacheDependency method.
The following example adds item to the cache with Oracle Dependency through stored procedure using the Insert method. The Insert
method adds a new item with dependency and if the item already exists in the cache it overwrites its properties.
try
{
// Create a connection string to establish connection with the database
// Connection String is in <AppSettings> in App.config
string connectionString = ConfigurationManager.AppSettings["connectionstring"];
// The name of the stored procedure the item is dependent on
string storedProcName = "GetProductByID";
OracleCmdParams param = new OracleCmdParams();
param.Type = (OracleCmdParamsType.Int16);
param.Value = 1020;
param.Direction = OracleParameterDirection.Input;
var oracleCmdParams = new Dictionary<string, OracleCmdParams>();
oracleCmdParams.Add("productID", param);
// Create Oracle Dependency
var oracleDependency = new OracleCacheDependency(connString, storedProcName, OracleCommandType.StoredProcedure, oracleCmdParams);
// Get product from database against given product ID
Product product = FetchProductFromDB(param.Value);
// Generate a unique cache key for this product
string key = $"Product:{product.ProductID}";
// Create a new cacheitem and add Oracle dependency to it
var cacheItem = new CacheItem(product);
cacheItem.Dependency = oracleDependency;
// Add cacheitem in the cache with Oracle Dependency
cache.Insert(key, cacheItem);
// For successful addition of item with Oracle Dependency
// Update the record in the database and check if key is present
// This can be done by using
// cache.Contains()
// cache.Count
}
catch (OperationFailedException ex)
{
// Exception can occur due to:
// Connection Failures
// Operation Timeout
// Operation performed during state transfer
}
catch (Exception ex)
{
// Any generic exception like ArgumentNullException or ArgumentException
// Exception may occur due to incorrect query format or invalid connection string
}
Recommendation: To ensure the operation is fail safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
Additional Resources
NCache provides sample application for Oracle dependency at:
- GitHub
- Shipped with NCache: %NCHOME%\samples\dotnet\Dependencies\OracleDependency
See Also
Sync Cache with SQL Server
Cache Data Dependency on OleDB
CLR Procedures in SQL Server with Cache
Locking Data For Concurrency Control
Cache Data Dependency on External Source