EclipseLink supports a shared (L2) object cache that avoids database access for objects and their relationships. This cache is enabled by default which is normally not a problem, unless the database is modified directly by other applications, or by the same application on other servers in a clustered environment.
There are many solutions to caching in a clustered environment, including:
- disable the shared cache
- only cache read-only objects
- set a cache invalidation timeout
- use refreshing on objects/queries when fresh data is required
- use optimistic locking (writes on stale data will fail, and will automatically invalidate the cache)
- using a distributed cache (such as Oracle TopLink Grid with Oracle Coherence)
- using cache coordination (synchronizing the caches, as discussed in this example)
This example gives an overview of the cache coordination option.
EclipseLink provides a cache coordination feature that enables a set of EclipseLink sessions to synchronize their changes in a distributed network such as an application server cluster. Cache coordination works by each EclipseLink session (ServerSession/persistence unit) on each server in the cluster being able to broadcast notification of transactional object changes to the other EclipseLink sessions in the cluster. EclipseLink supports cache coordination over RMI and JMS. The cache coordination framework is also extensible so other options could be developed.
This example demonstrates enabling cache coordination using JMS or RMI for a JEE EJB 3.0 SessionBean application using JPA deployed to an Oracle Weblogic cluster. The example could be ported to other JEE application servers or environments. EclipseLink supports cache coordination in any Java environment, including Java SE, or Tomcat.
If you encounter any issues in running this example, or are running in another app server or version please discuss, here
The following software is required to run this example:
- Oracle Weblogic Server (10.3.3 was used, but any WLS version supporting EJB 3 should work, or any JEE application server with some work) - download link
- ant (1.7 was used, but other versions should also work) - download link
- Oracle database (or any other client/server database (embedded database will not work as cannot be accessed from all machines)) - download link
- EclipseLink (WLS 10.3.4 includes EclipseLink 2.1, a 2.2 build or any >= 1.2 version could be used) - download link
- Cache coordination example - download link
Running the example
- Install Oracle WebLogic (or your desired JEE application server, or use existing application server)
- Upgrade the eclipselink.jar in your application server to be the latest release (not required)
- In WebLogic the eclipselink.jar is in <weblogic-home>/modules/ directory name org.eclipse.persistence_220.127.116.11_x-x.jar, where x depends on the versions on WebLogic.
- If using another application server you may need to add the eclipselink.jar to your application server's library path.
- Configure paths to server in the <example>/build.xml
- Install Oracle database (or use existing database, ensure the database is not an embedded database)
- Configure database driver, URL and user/password in <example>/build.xml (or create your own DataSource in your application server)