Java applications are very popular in high traffic environments. Many web applications, web services applications, and other server-side applications are developed in Java. And, these applications typically process a lot of data coming from a relational database.
But databases quickly become a scalability bottleneck as transaction load increases. And, in-memory data grids are becoming increasing popular for caching data in memory and reducing those expensive database trips so as to remove those bottlenecks. These in-memory data grids can easily scale out and do not become scalability bottleneck.
But, using an in-memory data grid means that you are caching data coming from the database and hence there are two copies of this data. This can cause data integrity problem where data in the database is updated but the in-memory data grid still has the older version of the data. To avoid this issue, you have to synchronize your cached data with the database to ensure that it is always correct.
This is an area that is typically neglected by most in-memory data grids. As a result, developers are hesitant to cache a lot of transactional data because they’re afraid it’ll become stale. And, without caching transactional data, your Java application cannot truly take advantage of an in-memory data grid because most of the data in a typical Java application is transactional.
To handle this problem, JvCache provides database synchronization of the in-memory data grid with Oracle databases. This ensures that even if a third party application modifies data in the database, the in-memory data grid is always kept synchronized with the database.
To do this synchronization, JvCache becomes an Oracle database client and uses Oracle notifications to register interest with the database against a given data set. This data set is basically the data that corresponds to the cached item. The database then triggers a notification whenever data in this data set changes. JvCache catches this notification and either invalidates the corresponding cached item or reloads it from the database by calling a Read-through handler.
JvCache encapsulates all that database synchronization capability inside an Oracle Cache Dependency feature. This way, you don’t have to implement anything to achieve this synchronization.
Oracle Cache Dependency is a great feature provided by JvCache to ensure that your in-memory data grid is always kept synchronized with your database. All you need to do is enable change notifications on your database and after that add your data in cache with OracleCacheDependency having information of database connection string and query.
JvCache creates a connection with your database by providing a connection string and registering your query with the Oracle database. So when any change happens to the related data set of the registered query Oracle database triggers change notification to JvCache. Later on JvCache removes the cached item with OracleCacheDependency from cache so that next time that item should be reloaded in cache with updated values.
Here are the simple steps to use OracleCacheDependency in your Java application:
1. Enable database change notifications to your user by executing this query. You just have to execute it once.
[SQL] "grant change notification to scott"
2. Import following jars in your application.
import com.alachisoft.jvache.web.caching.*; import com.alachisoft.jvcache.runtime.dependencies.*;
3. Now initialize your in-memory data grid, create OracleCacheDependency and add it with your item.
Cache cache = JvCache.initializeCache("oracleDependencyCache"); cache.clear(); String connectionString = "User Id=scott;Password=test;Data Source=oracle"; String query = "SELECT CustomerID FROM Customers WHERE CustomerID = 006"; CacheDependency orclSync = new OracleCacheDependency(connectionString, query); cache.insert("Customer:006", "myValue" , orclSync, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.Normal);
You can use JvCache and try it yourself to see how easily JvCache solves your database synchronization issues.