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.

−

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:

* 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 changes. Cache 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 cluster. EclipseLink supports cache coordination over RMI and JMS. The cache coordination framework is also extensible so other options could be developed.

+

The JPA <tt>Cache</tt> API and the EclipseLink <tt>JpaCache</tt> API can also be used directly to invalidate objects in the shared cache by the application. EclipseLink cache coordination could also be used to send invalidation messages to a cluster of EclipseLink persistence units.

−

Cache coordination works by broadcasting changes for each transaction to the other servers in the cluster. Each other server will receive the change notification, and either invalidate the changed objects in their cache, or update the cached objects state with the changes. Cache coordination occurs after the database commit, so only committed changes are broadcast.

+

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.

−

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 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).

−

===Configuring Cache Coordination===

+

EclipseLink DCN support is enabled through the <tt>OracleChangeNotificationListener</tt> 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 <tt>databaseChangeNotificationType</tt> attribute of the <tt>@Cache</tt> annotation to selectively disable change notification for certain classes.

−

Cache coordination is configured using persistence unit properties. The following cache coordination properties are supported:

+

+

Oracle DCN uses the ROWID to inform of row level changes. This requires EclipseLink to include the ROWID in all queries for a DCN enabled class. EclipseLink will also require to select the object's ROWID after an insert operation. EclipseLink must maintain a cache index on the ROWID, in addition to the object's Id. EclipseLink will also selects the database transaction id once each transaction to avoid invalidating the cache on the server that is processing the transaction.

* Changes to an object's secondary tables will not trigger it to be invalidate unless a version is used and updated in the primary table.

+

* Changes to an object's OneToMany, ManyToMany, and ElementCollection relationships will not trigger it to be invalidate unless a version is used and updated in the primary table.

+

+

===Configuring a Database Event Listener===

+

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.

Contents

Database Event Notification and Oracle DCN/QCN

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

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:

Use optimistic locking to ensure write consistency (writes on stale data will fail, and will automatically invalidate the cache).

The JPA Cache API and the EclipseLink JpaCache API can also be used directly to invalidate objects in the shared cache by the application. EclipseLink cache coordination could also be used to send invalidation messages to a cluster of EclipseLink persistence units.

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 attribute of the @Cache annotation to selectively disable change notification for certain classes.

Oracle DCN uses the ROWID to inform of row level changes. This requires EclipseLink to include the ROWID in all queries for a DCN enabled class. EclipseLink will also require to select the object's ROWID after an insert operation. EclipseLink must maintain a cache index on the ROWID, in addition to the object's Id. EclipseLink will also selects the database transaction id once each transaction to avoid invalidating the cache on the server that is processing the transaction.

EclipseLink's DCN integration has the following limitations:

Changes to an object's secondary tables will not trigger it to be invalidate unless a version is used and updated in the primary table.

Changes to an object's OneToMany, ManyToMany, and ElementCollection relationships will not trigger it to be invalidate unless a version is used and updated in the primary table.

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.