Difference between revisions of "EclipseLink/UserGuide/JPA/Basic JPA Development/Caching/DatabaseEvents"

From Eclipsepedia

Jump to: navigation, search
(EclipseLink persistence.xml cache coordination JMS example)
(Database Event Driven Cache Invalidation)
Line 17: Line 17:
 
*[[EclipseLink/Examples/JPA/DCN|How to use database event driven cache invalidation]]}}
 
*[[EclipseLink/Examples/JPA/DCN|How to use database event driven cache invalidation]]}}
 
=Database Event Driven Cache Invalidation=
 
=Database Event Driven Cache Invalidation=
Some databases and database products allow events to be raised from the database when rows are updated or deleted in the database.  The Oracle database supports Database Change Notification (DCN, QCN)
+
Some databases and database products allow events to be raised from the database when rows are updated or deleted in the database.
EclipseLink supports an API to allow the database to notify EclipseLink of database changes, so the changed objects can be invalidated in the EclipseLink shared cache.
+
  
EclipseLink provides cache coordination in a clustered environment to ensure the servers caches are is sync.
+
EclipseLink supports an API to allow the database to notify EclipseLink of database changes, so the changed objects can be invalidated in the EclipseLink shared cache.  This allows a shared cache to be used, and stale data to be avoided, even if other applications access the same data in the database.  EclipseLink supports integration with the Oracle Database feature for Database Change Notification (DCN, QCN).  A custom <tt>DatabaseEventListener</tt> may be provided for other databases and products that support database events.
  
There are also many other solutions to caching in a clustered environment, including:
+
There are also other solutions to caching in a shared environment, including:
 
* Disable the shared cache (through setting <code>@Cacheable(false)</code>, or <code>@Cache(isolation=ISOLATED)</code>).
 
* Disable the shared cache (through setting <code>@Cacheable(false)</code>, or <code>@Cache(isolation=ISOLATED)</code>).
 
* Only cache read-only objects.
 
* Only cache read-only objects.
Line 28: Line 27:
 
* Use refreshing on objects/queries when fresh data is required.
 
* Use refreshing on objects/queries when fresh data is required.
 
* Use optimistic locking to ensure write consistency (writes on stale data will fail, and will automatically invalidate the cache).
 
* Use optimistic locking to ensure write consistency (writes on stale data will fail, and will automatically invalidate the cache).
* Use a distributed cache (such as Oracle TopLink Grid's integration of EclipseLink with Oracle Coherence).
 
* Use database events to invalidate changed data in the cache (such as EclipseLink's support for Oracle DCN/QCN).
 
  
Cache coordination enables a set of persistence units deployed to different servers in the cluster (or on the same server) to synchronize their changesCache coordination works by each persistence unit on each server in the cluster being able to broadcast notification of transactional object changes to the other persistence units in the clusterEclipseLink supports cache coordination over RMI and JMS.  The cache coordination framework is also extensible so other options could be developed.
+
Database events can reduce the chance of an application getting stale data, but do not eliminate the possibility.  Optimistic locking should still be used to ensure data integrityEven in a single server application stale data is still possible within a persistence context unless pessimistic locking is usedOptimistic (or pessimistic) locking is always required to ensure data integrity in any multi-user system.
  
Cache coordination works by broadcasting changes for each transaction to the other servers in the clusterEach other server will receive the change notification, and either invalidate the changed objects in their cache, or update the cached objects state with the changesCache coordination occurs after the database commit, so only committed changes are broadcast.
+
==Oracle DCN/QCN==
 +
The Oracle database released a Database Change Notification (DCN) feature in the 10.2 releaseDCN allows for database events to be raised when the rows in a table are modifiedThe JDBC API for DCN was not complete until 11.2, so 11.2 is required for EclipseLink's integration.  DCN is also now referred to as QCN (Query Change Notification).
  
Cache coordination greatly reduces to chance of an application getting stale data, but does not eliminate the possibility.  Optimistic locking should still be used to ensure data integrityEven in a single server application stale data is still possible within a persistence context unless pessimistic locking is used.  Optimistic (or pessimistic) locking is always required to ensure data integrity in any multi-user system.
+
EclipseLink DCN support is enabled through the <tt>OracleChangeNotificationListener</tt> listener which integrates with Oracle JDBC to received database change eventsBy default all tables in the persistence unit are registered for change notification, but this can be configured using the <tt>databaseChangeNotificationType<tt> attribute of the <tt>@Cache</tt> annotation to selectively disable change notification for certain classes.
  
===Configuring Cache Coordination===
+
===Configuring a Database Event Listener===
Cache coordination is configured using persistence unit properties.  The following cache coordination properties are supported:
+
Database change events are configured using the persistence unit property <tt>eclipselink.cache.database-event-listener</tt>.  The full class name of the listener class must be provided, <tt>DCN</tt> or <tt>QCN</tt> can also be used to configure Oracle DCN.
  
 
+
======''EclipseLink persistence.xml Oracle DCN example''======
======''EclipseLink persistence.xml cache coordination JMS example''======
+
 
<source lang="xml">
 
<source lang="xml">
 
<?xml version="1.0" encoding="UTF-8"?>
 
<?xml version="1.0" encoding="UTF-8"?>
Line 52: Line 49:
 
         <exclude-unlisted-classes>false</exclude-unlisted-classes>
 
         <exclude-unlisted-classes>false</exclude-unlisted-classes>
 
         <properties>
 
         <properties>
             <property name="eclipselink.cache.coordination.protocol" value="jms"/>
+
             <property name="eclipselink.cache.database-event-listener" value="org.eclipse.persistence.platform.database.oracle.dcn.OracleChangeNotificationListener"/>
            <property name="eclipselink.cache.coordination.jms.topic" value="jms/ACMETopic"/>
+
            <property name="eclipselink.cache.coordination.jms.factory" value="jms/ACMETopicConnectionFactory"/>
+
 
         </properties>
 
         </properties>
 
     </persistence-unit>
 
     </persistence-unit>

Revision as of 14:36, 23 May 2012

EclipseLink JPA

Contents

Database Event Driven Cache Invalidation

Some databases and database products allow events to be raised from the database when rows are updated or deleted in the database.

EclipseLink supports an API to allow the database to notify EclipseLink of database changes, so the changed objects can be invalidated in the EclipseLink shared cache. This allows a shared cache to be used, and stale data to be avoided, even if other applications access the same data in the database. EclipseLink supports integration with the Oracle Database feature for Database Change Notification (DCN, QCN). A custom DatabaseEventListener may be provided for other databases and products that support database events.

There are also other solutions to caching in a shared environment, including:

  • Disable the shared cache (through setting @Cacheable(false), or @Cache(isolation=ISOLATED)).
  • Only cache read-only objects.
  • Set a cache invalidation timeout to reduce stale data.
  • Use refreshing on objects/queries when fresh data is required.
  • Use optimistic locking to ensure write consistency (writes on stale data will fail, and will automatically invalidate the cache).

Database events can reduce the chance of an application getting stale data, but do not eliminate the possibility. Optimistic locking should still be used to ensure data integrity. Even in a single server application stale data is still possible within a persistence context unless pessimistic locking is used. Optimistic (or pessimistic) locking is always required to ensure data integrity in any multi-user system.

Oracle DCN/QCN

The Oracle database released a Database Change Notification (DCN) feature in the 10.2 release. DCN allows for database events to be raised when the rows in a table are modified. The JDBC API for DCN was not complete until 11.2, so 11.2 is required for EclipseLink's integration. DCN is also now referred to as QCN (Query Change Notification).

EclipseLink DCN support is enabled through the OracleChangeNotificationListener listener which integrates with Oracle JDBC to received database change events. By default all tables in the persistence unit are registered for change notification, but this can be configured using the databaseChangeNotificationType<tt> attribute of the <tt>@Cache annotation to selectively disable change notification for certain classes.

Configuring a Database Event Listener

Database change events are configured using the persistence unit property eclipselink.cache.database-event-listener. The full class name of the listener class must be provided, DCN or QCN can also be used to configure Oracle DCN.

EclipseLink persistence.xml Oracle DCN example
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xsi:schemaLocation="http://java.sun.com/xml/ns/persistence persistence_2_0.xsd"
                version="2.0">
    <persistence-unit name="acme" transaction-type="RESOURCE_LOCAL">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <properties>
            <property name="eclipselink.cache.database-event-listener" value="org.eclipse.persistence.platform.database.oracle.dcn.OracleChangeNotificationListener"/>
        </properties>
    </persistence-unit>
</persistence>

Eclipselink-logo.gif
Version: 2.4 DRAFT
Other versions...