Loading
Understanding EclipseLink, 2.4
  Go To Table Of Contents
Contents
 Search
Other Searches
 PDF
PDF
Comments
Comments

About Cache Coordination

The need to maintain up-to-date data for all applications is a key design challenge for building a distributed application. The difficulty of this increases as the number of servers within an environment increases. EclipseLink provides a distributed cache coordination feature that ensures data in distributed applications remains current.

Cache coordination reduces the number of optimistic lock exceptions encountered in a distributed architecture, and decreases the number of failed or repeated transactions in an application. However, cache coordination in no way eliminates the need for an effective locking policy. To effectively ensure working with up-to-date data, cache coordination must be used with optimistic or pessimistic locking. Oracle recommends that you use cache coordination with an optimistic locking policy.

You can use cache invalidation to improve cache coordination efficiency. For more information, see About Cache Expiration and Invalidation.

As Figure 9-2 shows, cache coordination is a session feature that allows multiple, possibly distributed, instances of a session to broadcast object changes among each other so that each session's cache is either kept up-to-date or notified that the cache must update an object from the data source the next time it is read.


NoteNote:

You cannot use isolated client sessions with cache coordination. For more information, see Shared, Isolated, Protected, Weak, and Read-only Caches.


Figure 9-2 Coordinated Session Caches

Description of Figure 9-2 follows
Description of "Figure 9-2 Coordinated Session Caches"

When sessions are distributed, that is, when an application contains multiple sessions (in the same JVM, in multiple JVMs, possibly on different servers), as long as the servers hosting the sessions are interconnected on the network, sessions can participate in cache coordination. Coordinated cache types that require discovery services also require the servers to support User Datagram Protocol (UDP) communication and multicast configuration. For more information, see Coordinated Cache Architecture and Types.

This section describes the following:

When to Use Cache Coordination

Cache coordination can enhance performance and reduce the likelihood of stale data for applications that have the following characteristics:

  • Changes are all being performed by the same Java application operating with multiple, distributed sessions

  • Primarily read-based

  • Regularly requests and updates the same objects

To maximize performance, avoid cache coordination for applications that do not have these characteristics.

For other options to reduce the likelihood of stale data, see About Handling Stale Data.

Coordinated Cache Architecture and Types

You can configure a coordinated cache to broadcast changes using any of the following communication protocols:

JMS Coordinated Cache

For a JMS coordinated cache, when a particular session's coordinated cache starts up, it uses its JNDI naming service information to locate and create a connection to the JMS server. The coordinated cache is ready when all participating sessions are connected to the same topic on the same JMS server. At this point, sessions can start sending and receiving object change messages. You can then configure all sessions that are participating in the same coordinated cache with the same JMS and JNDI naming service information.

Example 9-1 illustrates a persistence.xml file configured for a JMS coordinated cache.

Example 9-1 persistence.xml File for JMS Cache Coordination

<?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.coordination.protocol" value="jms"/>
            <property name="eclipselink.cache.coordination.jms.topic" value="jms/ACMETopic"/>
            <property name="eclipselink.cache.coordination.jms.factory" value="jms/ACMETopicConnectionFactory"/>
        </properties>
    </persistence-unit>
</persistence>

For more information on configuring JMS, see "Configuring JMS Cache Coordination Using Persistence Properties" in Solutions Guide for EclipseLink. See also your JMS provider's documentation.

RMI Coordinated Cache

For an RMI coordinated cache, when a particular session's coordinated cache starts up, the session binds its connection in its naming service (either an RMI registry or JNDI), creates an announcement message (that includes its own naming service information), and broadcasts the announcement to its multicast group. When a session that belongs to the same multicast group receives this announcement, it uses the naming service information in the announcement message to establish bidirectional connections with the newly announced session's coordinated cache. The coordinated cache is ready when all participating sessions are interconnected in this way, at which point sessions can start sending and receiving object change messages. You can then configure each session with naming information that identifies the host on which the session is deployed.

Example 9-2 illustrates a persistence.xml file configured for a RMI coordinated cache.

Example 9-2 persistence.xml File for RMI Cache Coordination

<?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.coordination.protocol" value="rmi"/>
        </properties>
    </persistence-unit>
</persistence>

For more information, see "Configuring RMI Cache Coordination Using Persistence Properties" in Solutions Guide for EclipseLink.

Custom Coordinated Cache

Using the classes in org.eclipse.persistence.sessions.coordination package, you can define your own coordinated cache for custom solutions.

Coordinated Cache and Clustering

An application cluster is a set of middle tier server machines or VMs servicing requests for a single application, or set of applications. Multiple servers are used to increase the scalability of the application and/or to provide fault tolerance and high availability. Typically the same application will be deployed to all of the servers in the cluster and application requests will be load balanced across the set of servers. The application cluster will access a single database, or a database cluster. An application cluster may allow new servers to be added to increase scalability, and for servers to be removed such as for updates and servicing.

Application clusters can consist of Java EE servers, Web containers, or Java server applications.

EclipseLink can function in any clustered environment. The main issue in a clustered environment is utilizing a shared persistence unit (L2) cache. If you are using a shared cache (enabled by default in EclipseLink projects), then each server will maintain its own cache, and each caches data can get out of sync with the other servers and the database.

EclipseLink provides cache coordination in a clustered environment to ensure the servers caches are is sync.

There are also many other solutions to caching in a clustered 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).

  • Use database events to invalidate changed data in the cache (such as EclipseLink's support for Oracle Query Change Notification).

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.

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.

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.

Persistence Property Extensions for Cache Coordination

EclipseLink includes the following persistence property extensions for caching. For more information on these extensions, see Java Persistence API (JPA) Extensions Reference for EclipseLink.

  • cache.coordination.channel

  • cache.coordination.jms.factory

  • cache.coordination.jms.host

  • cache.coordination.jms.reuse-topic-publisher

  • cache.coordination.jms.topic

  • cache.coordination.jndi.initial-context-factory

  • cache.coordination.jndi.password

  • cache.coordination.jndi.user

  • cache.coordination.naming-service

  • cache.coordination.propagate-asynchronously

  • cache.coordination.protocol

  • cache.coordination.remove-connection-on-error

  • cache.coordination.rmi.announcement-delay

  • cache.coordination.rmi.multicast-group

  • cache.coordination.rmi.multicast-group.port

  • cache.coordination.rmi.packet-time-to-live

  • cache.coordination.rmi.url

  • cache.coordination.thread.pool.size

Cache Coordination and Oracle WebLogic

Both RMI and JMS cache coordination work with Oracle WebLogic. When a WebLogic cluster is used JNDI is replicated among the cluster servers, so a cache.coordination.rmi.url or a cache.coordination.jms.host option is not required. For JMS cache coordination, the JMS topic should only be deployed to only one of the servers (as of Oracle WebLogic 10.3.6). It may be desirable to have a dedicated JMS server if the JMS messaging traffic is heavy.

Use of other JMS services in WebLogic may have other requirements.

Cache Coordination and Glassfish

JMS cache coordination works with Glassfish. When a Glassfish cluster is used, JNDI is replicated among the cluster servers, so a cache.coordination.jms.host option is not required.

Use of other JMS services in Glassfish may have other requirements.

RMI cache coordination does not work when the JNDI naming service option is used in a Glassfish cluster. RMI will work if the eclipselink.cache.coordination.naming-service option is set to rmi. Each server must provide its own eclipselink.cache.coordination.rmi.url option, either by having a different persistence.xml file for each server, or by setting the URL as a System property in the server, or through a customizer.

Cache Coordination and IBM WebSphere

JMS cache coordination may have issues on IBM WebSphere. Use of a Message Driven Bean (MDB) may be required to allow access to JMS. To use an MDB with cache coordination, set the eclipselink.cache.coordination.protocol option to the value jms-publishing. The application will also have to deploy an MDB that processes cache coordination messages in its EAR file.

Example 9-3 illustrates the Java code required to configure an MDB.

Example 9-3 Cache Coordination Message Driven Bean

@MessageDriven
public class JMSCacheCoordinationMDB implements MessageListener {
 
    private JMSTopicRemoteConnection connection;
 
    @PersistenceUnit(unitName="acme")
    private EntityManagerFactory emf;
 
    public void ejbCreate() {
        this.connection = new JMSTopicRemoteConnection(this.emf.unwrap(ServerSession.class).getCommandManager());
    }
 
    public void onMessage(Message message) {
        this.connection.onMessage(message);
    }
 
}
Comments powered by Disqus