This section includes information on the CacheStore/CacheLoader configuration.
The TopLink Grid CacheStore/CacheLoader configuration allows you to map classes using JPA, but use the Coherence API to interact with the Coherence cache. This allows Coherence to interact with the database.
You can also use asynchronous writing with the CacheStore/CacheLoader configuration, by using the Coherence "write behind" configuration.
In general:
Through a CacheLoader, all read operations get objects from the database. See "Reading Objects".
Through a CacheLoader, all write operations update the database. See "Writing Objects".
See "Examples" for detailed examples.
In the Coherence CacheStore/CacheLoader, all read queries are directed to the database by the TopLink CacheLoader
.
This figure illustrates a query in the Grid Read configuration:
Application issues a get
query.
By using a CacheLoader, Coherence will load the from TopLink.
TopLink will query the database.
In the Coherence CacheStore/CacheLoader, TopLink performs all database writes (insert, update, delete) through the CacheStore
.
This figure illustrates a query in the Grid Read configuration:
Application issues a put
query.
By using a CacheStore, Coherence will store the from TopLink.
TopLink will insert the object into the database.
In the cache configuration (coherence-cache-config.xml
) define the cache, as shown in this example.
Example 1 Configuring the Cache
<cache-config> <caching-scheme-mapping> <cache-mapping> <cache-name>Employee</cache-name> <scheme-name>distributed-eclipselink</scheme-name> </caching-scheme-mapping> </caching-scheme-mapping> <caching-schemes> <distributed-scheme> <scheme-name>distributed-eclipselink</scheme-name> <service-name>EclipseLinkJPA</service-name> <backing-map-scheme> <read-write-backing-map-scheme> <internal-cache-scheme> <local-scheme /> </internal-cache-scheme> <!-- Define the cache scheme --> <cachestore-scheme> <class-scheme> <!-- Since the client code is using Coherence API we need the "standalone" version of the cache loader --> <class-name>oracle.eclipselink.coherence.standalone.EclipseLinkJPACacheStore</class-name> <init-params> <init-param> <param-type>java.lang.String</param-type> <param-value>{cache-name}</param-value> </init-param> <init-param> <param-type>java.lang.String</param-type> <param-value>employee</param-value> </init-param> </init-params> </class-scheme> </cachestore-scheme> </read-write-backing-map-scheme> </backing-map-scheme> <autostart>true</autostart> </distributed-scheme> </caching-schemes></cache-config>
In Example 2, you set the employee information and add the new object to Coherence. This issues an INSERT to the CacheStore
.
Employee employee = new Employee(); employee.setId(NEW_EMP_ID); employee.setFirstName("John"); employee.setLastName("Doe"); // Putting a new object into Coherence will result in an INSERT in the CacheStore employeeCache.put(NEW_EMP_ID, employee);
Getting an object from the cache produces no SQL statements. Getting an object that is not in the cache produces a SELECT
statement.
// Getting an object from cache produces no SQL System.out.println("New Employee from cache is: " + employeeCache.get(NEW_EMP_ID)); // Getting an object not in cache will produce a SELECT in the CacheStore System.out.println("Non-existant Employee from cache is: " + employeeCache.get(NON_EXISTANT_EMP_ID));