high performance jpa with oracle coherence
DESCRIPTION
My little Doag2011 slide deck about how to integrate Oracle Coherence with JPATRANSCRIPT
msg systems ag, 17.10.2011 1 Markus Eisele, Insurance - Strategic IT-Architecture
msg systems ag, 17.10.2011 Markus Eisele, Insurance - Strategic IT-Architecture 2
Agenda
1. What is JPA?
2. How does JPA work?
3. What is Coherence?
4. How does Coherence work?
5. Why Coherence with JPA?
6. „JOTG“ - JPA On The Grid
1. JPA Backed Caches
2. JPA 2nd Level Cache
3. JPA 2nd Level Cache with JPA Backed Cache
7. Summary
msg systems ag, 17.10.2011 Markus Eisele, Insurance - Strategic IT-Architecture 3
http://blog.eisele.net
http://twitter.com/myfear
msg systems ag, 17.10.2011 Markus Eisele, Insurance - Strategic IT-Architecture 4
• The Java Persistence API (JPA) is a Java
programming language framework managing
relational data in applications
• The Java Persistence API originated as part of
the work of the JSR 220 Expert Group. JPA
2.0 is the work of the JSR 317 Expert Group.
• Persistence in this context covers three areas:
the API itself, defined in the javax.persistence
package
the Java Persistence Query Language (JPQL)
object/relational metadata
What is JPA?
msg systems ag, 17.10.2011 Markus Eisele, Insurance - Strategic IT-Architecture 5
Typical Providers
msg systems ag, 17.10.2011 Markus Eisele, Insurance - Strategic IT-Architecture 6
How does JPA work?
http://glassfish.java.net/javaee5/persistence/persistence-example.html
msg systems ag, 17.10.2011 Markus Eisele, Insurance - Strategic IT-Architecture 7
@Entity public class Customer { private int id; private String name; private Collection<Order> orders; @Id public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @OneToMany(cascade=ALL, mappedBy="customer") public Collection<Order> getOrders() { return orders; } public void setOrders(Collection<Order> newValue) { this.orders = newValue; } }
JPA Entities @Entity
@Table(name="ORDER_TABLE")
public class Order {
private int id;
private String address;
private Customer customer;
@Id
@Column(name="ORDER_ID")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(name="SHIPPING_ADDRESS")
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@ManyToOne()
@JoinColumn(name="CUSTOMER_ID")
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
}
msg systems ag, 17.10.2011 Markus Eisele, Insurance - Strategic IT-Architecture 8
// Create new customer
Customer customer0 = new Customer();
customer0.setId(1);
customer0.setName("Joe Smith");
// Persist the customer
em.persist(customer0);
// Associate orders with the customer.
customer0.getOrders().add(order1);
order1.setCustomer(customer0);
// Persist the customer
em.persist(customer0);
Working with the Entity Manager
msg systems ag, 17.10.2011 Markus Eisele, Insurance - Strategic IT-Architecture 9
What is Coherence?
msg systems ag, 17.10.2011 Markus Eisele, Insurance - Strategic IT-Architecture 10
How Coherence works
msg systems ag, 17.10.2011 Markus Eisele, Insurance - Strategic IT-Architecture 11
How Coherence works
msg systems ag, 17.10.2011 Markus Eisele, Insurance - Strategic IT-Architecture 12
How Coherence works
msg systems ag, 17.10.2011 Markus Eisele, Insurance - Strategic IT-Architecture 13
• Members have logical access to all Entities
At most 2 network operations per access
At most 4 network operations per update
Regardless of cluster size
Deterministic access and update behaviour
How Coherence works
msg systems ag, 17.10.2011 Markus Eisele, Insurance - Strategic IT-Architecture 14
• TopLink Grid is a component of Oracle TopLink
• TopLink Grid allows Java developers to transparently leverage the power of the Coherence data grid
• TopLink Grid combines: the simplicity of application development using the
Java standard Java Persistence API (JPA) with
the scalability and distributed processing power of Oracle’s Coherence Data Grid.
What is TopLink Grid?
msg systems ag, 17.10.2011 Markus Eisele, Insurance - Strategic IT-Architecture 15
• Supports 'JPA on the Grid' Architecture EclipseLink JPA applications using Coherence as a
shared (L2) cache replacement along with configuration for more advanced usage
TopLink Grid integrates EclipseLink JPA and Coherence
Base configuration uses Coherence data grid as distributed shared cache
Updates to Coherence cache immediately available to all cluster nodes
Advanced configurations uses data grid to process queries to avoid database access and decrease database load
What is TopLink Grid?
msg systems ag, 17.10.2011 Markus Eisele, Insurance - Strategic IT-Architecture 16
• Historical approach to scaling a JPA
application
Adding nodes to a cluster
Tuning database performance to reduce query time
• Both of these approaches will support
scalability but only to a point
Why Coherence with JPA?
msg systems ag, 17.10.2011 Markus Eisele, Insurance - Strategic IT-Architecture 17
• Historical approach to scaling EclipseLink JPA applications into a cluster:
Disable Shared Cache
Each transaction retrieves all required data from the database. Increased database load limits overall scalability but ensures all nodes have latest data.
Memory footprint of application increases as each transaction has a copy of each required Entity
Every transaction pays object construction cost for queried Entities.
Database becomes bottleneck
Cache Coordination
When Entity is modified in one node, other cluster nodes messaged to replicate/invalidate shared cached Entities.
Creation and/or modification of Entity results in message to all other nodes
Messaging latency means that nodes may have stale data for a short period.
Shared cache size limited by heap of each node
Objects shared across transactions to reduce memory footprint
Why Coherence with JPA?
msg systems ag, 17.10.2011 Markus Eisele, Insurance - Strategic IT-Architecture 18
Strategies for JPA on the grid
msg systems ag, 17.10.2011 Markus Eisele, Insurance - Strategic IT-Architecture 19
• Coherence API with caches backed by a
database mapped through JPA.
• The grid accesses relational data through JPA
CacheLoader and CacheStore
implementations.
• TopLink Grid provides CacheLoader and
CacheStore implementations that are
optimized for EclipseLink JPA.
(EclipseLinkJPACacheLoader and
EclipseLinkJPACacheStore)
• Using 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.
JPA Backed Caches – Traditional Approach
msg systems ag, 17.10.2011 Markus Eisele, Insurance - Strategic IT-Architecture 20
// Get the configured Cache
NamedCache cache =
CacheFactory.getCache("Customer");
//Create a new Customer
Customer cust = new Customer();
cust.setFirstName("Markus");
cust.setLastName("Eisele");
cust.setId(1);
//Put the Employee into the cache
cache.put(1, cust);
Using the Traditional Approach
msg systems ag, 17.10.2011 Markus Eisele, Insurance - Strategic IT-Architecture 21
• Ensures all nodes have coherent view of data.
Database is always right
Shared Cache is always right—Entities read, modified, or created are available to all cluster members.
• Communication is to primary and backup nodes.
• Coherence cache size is the sum of the available heap of all members—larger cache size enables longer tenure and better cache hit rate
• Can be used with existing applications and all EclipseLink performance features without altering application results
JPA Second Level Cache – Grid Cache
msg systems ag, 17.10.2011 Markus Eisele, Insurance - Strategic IT-Architecture 22
...
import
oracle.eclipselink.coherence.integrated.cache.Coherenc
eInterceptor;
import
org.eclipse.persistence.annotations.CacheInterceptor;
...
@Entity
@CacheInterceptor(value = CoherenceInterceptor.class)
public class Customer implements Serializable {
...
}
Using the 2nd Level Cache
msg systems ag, 17.10.2011 Markus Eisele, Insurance - Strategic IT-Architecture 23
• Grid Read (@Customizer(CoherenceReadCustomizer.class)) )
In the Grid Cache configuration, all reads (both pk and
non-pk) are executed against the grid (by default).
For Entities that typically:
Need to be highly available
Must have updates written synchronously to the database;
database is system of record
• Grid Entity (@Customizer(CoherenceReadWriteCustomizer.class))
The Grid Entity configuration is the same as the Grid
Read configuration except that all writes are executed
against the grid, not the database.
For Entities that typically:
May have updates written asynchronously to the database (if CacheStore configured)
Configuring Optimizations
msg systems ag, 17.10.2011 Markus Eisele, Insurance - Strategic IT-Architecture 24
• Combining both approaches is possible with
some combinations
• Grid Entity
Can be optionally used with CacheStore to
update the database.
• Grid Read
Can be optionally used with CacheLoader.
JPA 2nd Level Cache with JPA Backed Cache
msg systems ag, 17.10.2011 Markus Eisele, Insurance - Strategic IT-Architecture 25
• Coherence does not provide support for the serialization of complex graphs across caches.
Coherence serializes objects/object graphs and places the results in to a single cache under a key.
• TopLink Grid 11gR1 does support storage of complex graphs of Entities with each Entity type stored in a corresponding Coherence cache.
Relationship information is stored into Coherence
Relationships are reconstituted upon retrieval from Coherence
Lazy and eager relationships are supported
Relationships between Coherence cached and database persisted objects is supported.
• TopLink Grid wraps Entities with relationships with a byte code generated Wrapper class
when put()ing into Coherence
Wrapper encodes relationship details
Wrapper is stripped off when Entity retrieved from Coherence and relationships are
reconstituted
Eager relationships will result is retrieval (from either Coherence or database depending
on configuration) of target Entity/Entities
• Non-TopLink Grid applications can query Entities from Coherence with Filters and get()
wrapped Entities
TopLink Grid serializer must be configured on Cache
Coherence clients can configure auto-unwrapping
Relationships will be null when retrieved by Coherence clients
Relationship Support
msg systems ag, 17.10.2011 Markus Eisele, Insurance - Strategic IT-Architecture 26
• TopLink supports a range of strategies for scaling JPA
applications
• TopLink Grid integrates EclipseLink JPA with Oracle
Coherence to provide:
'JPA on the Grid' functionality to support scaling JPA
applications with Coherence
Support for caching Entities with relationships in
Coherence
• Both TopLink and Coherence are a part of WebLogic
Application Grid
Summary
msg systems ag, 17.10.2011 Markus Eisele, Insurance - Strategic IT-Architecture 27
• Watch out for Oracle Active Cache
Combination of Coherence with either WebLogic
Server or Oracle GlassFish Server.
Session State Persistence and Management
Accessing Java Persistence API (JPA) Entities in
the Data Cache
ActiveCache !!!
msg systems ag, 17.10.2011 Markus Eisele, Insurance - Strategic IT-Architecture 28
http://blog.eisele.net
http://www.oracle.com/technetwork/middleware/coherence/overview/index
.html
http://www.oracle.com/technetwork/middleware/toplink/overview/index.ht
ml
http://www.oracle.com/technetwork/middleware/toplink/tl-grid-097210.html
http://www.eclipselink.org/
http://java.sys-con.com/node/951117
Links and Readings
msg systems ag, 2011 Markus Eisele, Oracle ACE Director FMW & SOA 29
Disclaimer
The thoughts expressed here are
the personal opinions of the author
and no official statement
of the msg systems ag.
www.msg-systems.com
Thank you for your attention!
msg systems ag, 17.10.2011 Markus Eisele, Insurance - Strategic IT-Architecture 30
Markus Eisele
www.msg-systems.com