DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Data Engineering
  3. Data
  4. High Performance JPA with GlassFish and Coherence - Part 2

High Performance JPA with GlassFish and Coherence - Part 2

Markus Eisele user avatar by
Markus Eisele
·
Feb. 16, 11 · Interview
Like (0)
Save
Tweet
Share
8.36K Views

Join the DZone community and get the full member experience.

Join For Free
In this second part of my four part series I'll explain strategy number one of using Coherence with EclipseLink and GlassFish.This describes the steps you have to take to configure a JPA backed Cache with Coherence and how you could use it from within GlassFish as a high performance data store.

General approach
You can use the Coherence API with caches backed by a database mapped through JPA. The grid accesses relational data through JPA CacheLoader and CacheStore implementations. In this "traditional" Coherence approach, TopLink Grid provides CacheLoader and CacheStore implementations that are optimized for EclipseLink JPA. (EclipseLinkJPACacheLoader and EclipseLinkJPACacheStore) which are both contained in the toplink-grid.jar file. Going this way, you use the standard JPA run-time configuration file persistence.xml and the JPA mapping file orm.xml. The Coherence cache configuration file coherence-cache-config.xml must be specified to override the default Coherence settings and define the CacheStore caching scheme.

Details
The CacheLoader implementation  redirects all read operations/queries directly to the database via EclipseLink. The CacheStore takes care for all write operations (insert, update, delete) which are executed against the database using EclipseLink. This solution is basically independent from GlassFish and it's JPA provider. You simply use Coherence with it's native API and Coherence takes care of persisting the entities. This approach is referred to as "Traditional Coherence Configuration" in the documentation.

Preparation
In order to get this and each of the following examples to work, we need to make some preparations. First one is to get all the needed software. Get your copy of Java SE Development Kit 6u23, GlassFish 3.1 (Part of NetBeans), MySQL Community Server (5.5.9), TopLink (11.1.1.4.0) and Coherence (3.6.1.0). Any IDE would work. I based all examples on NetBeans 7.0 Beta2. You should start by installing the bits and pieces. Create an empty "coherence" db with MySQL. We will use this one for all examples. I will NOT guide you through this. I'll start with the relevant Coherence parts. This will be the configuration you need to follow the other parts of this series, too. So you'd better keep it ;)
Unzip your Coherence download to a suitable location (e.g. x:\Oracle\coherence\) and check the installation by setting up and running the sample cache server application. Change to the \bin folder and execute the cache-server.cmd/sh. Prior to this make sure, that the JAVA_HOME variable is pointing to a valid JDK install. If everything went smoothly you get message: "Started DefaultCacheServer...". One simple side note: Coherence is cluster-aware. Which means, it automatically tries to discover other running Coherence nodes in your subnet. If you are seeing any other output than "ActualMemberSet=MemberSet(Size=1," you should stop here and configure Coherence to run on your machine only. Great. All set. Let's go and execute the example client. Start coherence.cmd/sh and wait for the output "Map (?):". If you check back with the server console you will see something like this:
(thread=Cluster, member=1): Member 2 joined Service Management with senior member 1.
Play around with it and try the following commands, each confirmed with return: help, cache myCache, put message "hello", get message, size, list, bye. For further help please consult the tutorial.

Move it into practice
Start a new Java/Java Class Library project with NetBeans. Name it "GridEntityExample" (check the dedicated lib folder box). Add coherence.jar, toplink-grid.jar and mysql-connector-java-5.1.13-bin.jar to the lib folder and add them to the projects dependencies.
Add two new packages entities and server (e.g. net.eisele.coherence.entities, net.eisele.coherence.server) and
As we already have the cache-server example, it's easy to modify it to fit our needs.  Add a new Entity Class (Employee) to the entities package. Change the primary key type to "int" and click next. Select EclipseLink JPA 2.0 from the dropdown box. As the wizard will not let you finish without, create a new Database Connection (jdbc:mysql://localhost:3306/coherence) and click finish. Add two private Strings (firstName, lastName) and their getters and setters to the entity. Remove the @GeneratedValue from the id attribute (more on that later). Some tweaks in the persistence.xml finish up the configuration. Add the following:

<property name="eclipselink.ddl-generation" value="create-tables" />
<property name="eclipselink.ddl-generation.output-mode"
value="database" />
<property name="eclipselink.jdbc.read-connections.min" value="1" />
<property name="eclipselink.jdbc.write-connections.min" value="1" />
<property name="eclipselink.jdbc.batch-writing" value="JDBC" />
<property name="eclipselink.logging.level" value="FINE" />
<property name="eclipselink.logging.thread" value="false" />
<property name="eclipselink.logging.session" value="false" />
<property name="eclipselink.logging.exceptions" value="false" />
<property name="eclipselink.logging.timestamp" value="false"/>


Now we need a simple StartCacheServer class which should resist in the server package. Give it a simple main method:

public static void main(String[] args) throws UnknownHostException {
// Restrict Coherence to localhost
System.setProperty("tangosol.coherence.wka", InetAddress.getLocalHost().getHostAddress());
// Start CacheServer
DefaultCacheServer.main(new String[0]);
}


Now we have to configure Coherence. This is done via the coherence-cache-config.xml file. The complete file can be found in the tutorial. The key to success is the second init-param for the class-scheme. You have to reference the persistence unit from the persistence.xml there:

<init-param>
<param-type>java.lang.String</param-type>
<param-value>GridEntityExamplePU</param-value>
</init-param>


Right-click StartCacheServer and run it. You should see the already know message: "Started DefaultCacheServer...".
Ok. Now we are going to insert our first employee into the database. Create a simple InsertExample class:

public static void main(String[] args) throws UnknownHostException {
// Restrict Coherence to localhost
System.setProperty("tangosol.coherence.wka", InetAddress.getLocalHost().getHostAddress());
// Disable local storage--need to start independent CacheServer
System.setProperty("tangosol.coherence.distributed.localstorage", "false");
// Get the configured Cache
NamedCache cache = CacheFactory.getCache("Employee");
//Create a new Employee
Employee emp = new Employee();
emp.setFirstName("Markus");
emp.setLastName("Eisele");
emp.setId(1);
//Put the Employee into the cache
cache.put(1, emp);
}


Run it and check with your db, if the newly created Employee is there. It should be :)
Done. That's all. Ok, where exactly is the GlassFish part?

Using this approach with GlassFish
It's simple as hell. Create a new domain (let's call it coherence) and create a new WebApplication (lets call it GridEntityWeb). Add the GridEntityExample project and the coherence.jar as dependencies. Now you have to configure the cache. This is done again via a coherence-cache-config.xml. Create one in WEB-INF/classes and make it reference the EclipseLinkJPA service-name.

<cache-config>
<caching-scheme-mapping>
<cache-mapping>
<cache-name>*</cache-name>
<scheme-name>distributed-eclipselink</scheme-name>
</cache-mapping>
</caching-scheme-mapping>

<caching-schemes>
<distributed-scheme>
<scheme-name>distributed-eclipselink</scheme-name>
<service-name>EclipseLinkJPA</service-name>
</distributed-scheme>

</caching-schemes>
</cache-config>



That's all. Make sure the StartCacheServer is running and fire up your GlassFish domain together with your application. If you simply copy the few lines from the InsertExample you can quickly test if everything is working. All left to do now is to find a nice and clean way to integrate this into your application. Maybe you could come up with a generic Coherence crud service (probably based on Adam's proposal).

Drawbacks and thoughts
When using this approach, it is essential to configure the persistence unit to ensure that no changes are made to entities when they are inserted or updated. Any changes made to entities by the JPA provider are not reflected in the Coherence cache. This means that the entity in the cache will not match the database contents. In particular, entities should not use ID generation, for example, @GeneratedValue, to obtain an ID. That means that in order to insert an object into a Coherence cache, you need to provide its identity as well, to serve as the key.
The ID is typically the key under which the entity is stored in Coherence. Optimistic locking (for example, @Version) should not be used as it may lead to a database transaction commit failure.
So in general this is not really a commonly use approach which only fits with some special scenarios where you are fighting with big loads of data.
But what exactly is the deal here. Let's see: If you run the configured example in a loop and insert 12.635 Employee objects, it takes roughly 13 minutes on my machine. This happens if you run Coherence in synchronous mode. But you can utilize a feature called "write-behind" to improve application response time by performing database updates asynchronously. If you add one single line to the CacheServer's coherence-cache-config.xml:

<write-delay>10s</write-delay>

the needed time to issue the inserts from an application point of view shrinks to 9 seconds! Of course, the running server still needs it's time to process the updates asynchronously but for the client this is the optimal behavior.

Enough for now. I will try to explain more an more concepts running my way through the examples. As you might have guessed, there is a lot to know if you work with caches. Next part will cover Coherence as L2 cache for EclipseLink with GlassFish. Thanks for reading. Stay tuned.

Further Readings:
Using Coherence Caches Backed by TopLink Grid
Coherence 3.6 Tutorial
Synthetic Sequence Approach

From http://blog.eisele.net/2011/02/high-performance-jpa-with-glassfish-and_14.html

Coherence (units of measurement) Database connection GlassFish Relational database Cache (computing) application EclipseLink IT Native API

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • HTTP vs Messaging for Microservices Communications
  • Custom Validators in Quarkus
  • Monolithic First
  • Key Elements of Site Reliability Engineering (SRE)

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: