Digital Identity is Under Attack: FIDO Paris Seminar.pptx
High performance JPA with Oracle Coherence
1. 1 Markus Eisele, Insurance - Strategic IT-Architecture msg systems ag, 17.10.2011
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
2 Markus Eisele, Insurance - Strategic IT-Architecture msg systems ag, 17.10.2011
4. What is JPA?
• 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
4 Markus Eisele, Insurance - Strategic IT-Architecture msg systems ag, 17.10.2011
5. Typical Providers
5 Markus Eisele, Insurance - Strategic IT-Architecture msg systems ag, 17.10.2011
6. How does JPA work?
http://glassfish.java.net/javaee5/persistence/persistence-example.html
6 Markus Eisele, Insurance - Strategic IT-Architecture msg systems ag, 17.10.2011
7. JPA Entities @Entity
@Table(name="ORDER_TABLE")
public class Order {
private int id;
@Entity private String address;
public class Customer { private Customer customer;
private int id;
private String name; @Id
private Collection<Order> orders; @Column(name="ORDER_ID")
public int getId() {
@Id return id;
public int getId() {
return id; }
}
public void setId(int id) {
public void setId(int id) { this.id = id;
this.id = id;
} }
public String getName() { @Column(name="SHIPPING_ADDRESS")
return name; public String getAddress() {
}
return address;
public void setName(String name) { }
this.name = name;
} public void setAddress(String address) {
@OneToMany(cascade=ALL, mappedBy="customer") this.address = address;
public Collection<Order> getOrders() { }
return orders;
} @ManyToOne()
public void setOrders(Collection<Order> newValue) { @JoinColumn(name="CUSTOMER_ID")
this.orders = newValue; public Customer getCustomer() {
} return customer;
} }
public void setCustomer(Customer customer) {
this.customer = customer;
}
}
7 Markus Eisele, Insurance - Strategic IT-Architecture msg systems ag, 17.10.2011
8. Working with the Entity Manager
// 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);
8 Markus Eisele, Insurance - Strategic IT-Architecture msg systems ag, 17.10.2011
9. What is Coherence?
9 Markus Eisele, Insurance - Strategic IT-Architecture msg systems ag, 17.10.2011
10. How Coherence works
10 Markus Eisele, Insurance - Strategic IT-Architecture msg systems ag, 17.10.2011
11. How Coherence works
11 Markus Eisele, Insurance - Strategic IT-Architecture msg systems ag, 17.10.2011
12. How Coherence works
12 Markus Eisele, Insurance - Strategic IT-Architecture msg systems ag, 17.10.2011
13. How Coherence works
• 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
13 Markus Eisele, Insurance - Strategic IT-Architecture msg systems ag, 17.10.2011
14. What is TopLink Grid?
• 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.
14 Markus Eisele, Insurance - Strategic IT-Architecture msg systems ag, 17.10.2011
15. What is TopLink Grid?
• 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
15 Markus Eisele, Insurance - Strategic IT-Architecture msg systems ag, 17.10.2011
16. Why Coherence with JPA?
• 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
16 Markus Eisele, Insurance - Strategic IT-Architecture msg systems ag, 17.10.2011
17. Why Coherence with JPA?
• 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
17 Markus Eisele, Insurance - Strategic IT-Architecture msg systems ag, 17.10.2011
18. Strategies for JPA on the grid
18 Markus Eisele, Insurance - Strategic IT-Architecture msg systems ag, 17.10.2011
19. JPA Backed Caches – Traditional Approach
• 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.
19 Markus Eisele, Insurance - Strategic IT-Architecture msg systems ag, 17.10.2011
20. Using the Traditional Approach
// 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);
20 Markus Eisele, Insurance - Strategic IT-Architecture msg systems ag, 17.10.2011
21. JPA Second Level Cache – Grid Cache
• 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
21 Markus Eisele, Insurance - Strategic IT-Architecture msg systems ag, 17.10.2011
22. Using the 2nd Level Cache
...
import
oracle.eclipselink.coherence.integrated.cache.Coherenc
eInterceptor;
import
org.eclipse.persistence.annotations.CacheInterceptor;
...
@Entity
@CacheInterceptor(value = CoherenceInterceptor.class)
public class Customer implements Serializable {
...
}
22 Markus Eisele, Insurance - Strategic IT-Architecture msg systems ag, 17.10.2011
23. Configuring Optimizations
• 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)
23 Markus Eisele, Insurance - Strategic IT-Architecture msg systems ag, 17.10.2011
24. JPA 2nd Level Cache with JPA Backed Cache
• 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.
24 Markus Eisele, Insurance - Strategic IT-Architecture msg systems ag, 17.10.2011
25. Relationship Support
• 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
25 Markus Eisele, Insurance - Strategic IT-Architecture msg systems ag, 17.10.2011
26. Summary
• 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
26 Markus Eisele, Insurance - Strategic IT-Architecture msg systems ag, 17.10.2011
27. ActiveCache !!!
• 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
27 Markus Eisele, Insurance - Strategic IT-Architecture msg systems ag, 17.10.2011
28. Links and Readings
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
28 Markus Eisele, Insurance - Strategic IT-Architecture msg systems ag, 17.10.2011
29. Disclaimer
The thoughts expressed here are
the personal opinions of the author
and no official statement
of the msg systems ag.
29 Markus Eisele, Oracle ACE Director FMW & SOA msg systems ag, 2011
30. Thank you for your attention!
Markus Eisele
markus.eisele@msg-systems.com
www.msg-systems.com
www.msg-systems.com
30 Markus Eisele, Insurance - Strategic IT-Architecture msg systems ag, 17.10.2011