introduction to jpa and toplink grid
DESCRIPTION
TRANSCRIPT
<Insert Picture Here>
TopLink Grid: Scaling JPA applications with
Coherence
Andrei Cristian Niculae
Technology Presales Consultant
The following is intended to outline our general
product direction. It is intended for information
purposes only, and may not be incorporated into any
contract. It is not a commitment to deliver any
material, code, or functionality, and should not be
relied upon in making purchasing decisions.
The development, release, and timing of any
features or functionality described for Oracle‟s
products remains at the sole discretion of Oracle.
2
JPA: Java Persistence APIJAXB: Java Architecture for XML BindingSDO: Service Data ObjectsDBWS: Database WebServices
Java Persistence: The Problem Space
CUST
ID NAME C_RATING
Relational
Customer
id: int
name: String
creditRating: int
Java
<customer id=“…”>
<name>…</name>
<contact-info>
…
</contact-info>
</customer>
XML
JPA
JAXB/SDO
DBWS
Oracle TopLink 11gR1
• Comprehensive Persistence Solution• Java Persistence (JPA 2.0 Reference Implementation), Java Architecture for XML
Binding (JAXB 2.x), Database Web Service (DBWS)
• Strong Developer Support
• Developed in open source EclipseLink project at Eclipse.org
• Tool support in OEPE (Eclipse EE), NetBeans, JDeveloper
• Widely Used
• The enterprise persistence provider for WebLogic and GlassFish
• EclipseLink distributed with Spring, JOnAS, NetWeaver (soon)
• Performance
• Key contributor to Oracle's SpecJ World Record
• Superior results across the board against key competitors in internal
benchmarks
• Best Java Persistence for the Oracle DB and other DBs
Java Persistence API (JPA)—in a
Nutshell
• A Java standard that defines:
• how Java objects are stored in relational databases (specified
using a standard set of mappings)
• a programmer API for reading, writing, and querying
persistent Java objects (“Entities”)
• a full featured query language
• a container contract that supports plugging any JPA runtime
in to any compliant container.
JPA—Background
• JPA 1.0 part of EJB 3.0 JSR 220
• JPA 2.0 standalone JSR 317
• JPA 2.1 (JSR 338) included in Java EE 7
• Suitable for use in different modes
• Standalone in Java SE environment
• Hosted within a Java EE Container
• Standardization of current persistence practices
• Merging of expertise from persistence vendors and
communities including: TopLink, Hibernate, JDO, EJB
vendors and individuals
JPA—POJO Entities
• Abstract or concrete top level Java class
• Non-final fields/properties, no-arg constructor
• No required interfaces
• No required business or callback interfaces (but you may
use them if you want to)
• Direct field or property-based access
• Getter/setter can contain logic (e.g. for validation)
• May be Serializable, but not required
• Only needed if passed by value (in a remote call)
Mapping
• The activity of „Mapping‟ is the process of connecting
objects/attributes to tables/columns
id: int
name: String
creditRating: int
Customer CUST
ID NAME C_RATING
JPA Object-Relational Mappings
• Core JPA Mappings
• Id
• Basic
• Relationships
• OneToOne
• ManyToOne
• OneToMany
• ManyToMany
• And more…
• Annotations and/or XML
Annotations on Fields
@Entity
public class Customer {
@Id
private String name;
@OneToOne
private Account account;
public String getName() { return name; }
public void setName(String name) {
this.name = name;
}
public Account getAccount() { return account; }
public void setAccount(Account account) {
this.account = account;
}
}
Annotations on Properties
@Entity
public class Customer {
private String name;
private Account account;
@Id
public String getName() { return name; }
public void setName(String name) {
this.name = name;
}
@OneToOne
public Account getAccount() { return account; }
public void setAccount(Account account) {
this.account = account;
}
}
Mappings in XML
<entity-mappings
xmlns="http://java.sun.com/xml/ns/persistence/orm"
…
<entity class=“Customer">
<attributes>
<id name=“name“/>
<one-to-one name=“account“/>
</attributes>
</entity>
…
</entity-mappings>
EclipseLink JPA Developer Tool
Support
Eclipse IDE
EclipseLink support included by Dali in Eclipse WTP
Dali supports download and install of EclipseLink into IDE
Oracle Enterprise Pack for Eclipse (OEPE)
JDeveloper 11g
JPA, JAXB, Native ORM, and Native OXM
NetBeans
Example JPA Client Code
…
Mechanics of a JPA Application
ReadEntities
Step 1
Application Logic
JPA
Create/ModifyEntities
Step 2
Application Logic
JPA JPA
Insert/UpdateEntities
Step 3
Application Logic
JPA
JPA with Shared Cache
TX Commit
Application Logic
TX n
Application Logic
TX n+1
ReadEntities
Cache hits avoid object build cost
Scaling Java Persistence
Ap
plic
ati
on
Ap
plic
ati
on
Ap
plic
ati
on
Ap
plic
ati
on
Ap
plic
ati
on
JPAA
pp
lic
ati
on
Ap
pli
ca
tio
n
Ap
plic
ati
on
Ap
plic
ati
on
Ap
plic
ati
on
JPA
Ap
plic
ati
on
Ap
pli
ca
tio
n
Ap
plic
ati
on
Ap
plic
ati
on
Ap
plic
ati
on
JPA. . .
EclipseLink in a Cluster
Application Logic
EntityManager
EntityManagerFactory
Shared
Cache
Persistence
Context
Application Logic
EntityManager
EntityManagerFactory
Shared
Cache
Persistence
Context
Need to keep
Shared Caches
Coherent
Traditional Approaches to Scaling JPA
• Historically there have been two strategies for scaling EclipseLink JPA applications into a cluster:
• Disable Shared Caching
• No shared cache
• Always Refresh
• Cache Coordination
• Communicate changes via messaging
Strategy 1: Disable Shared Cache
EntityManagerFactory EntityManagerFactory
Application Logic
EntityManager
Persistence
Context
Application Logic
EntityManager
Persistence
Context
Disable Shared Cache
• Ensures all nodes have coherent view of data.
• Database is always right
• Each transaction queries all required data from database and
constructs Entities
• No inter-node messaging
• 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
Strategy 2: Cache Coordination
Application Logic
EntityManager
EntityManagerFactory
Shared
Cache
Persistence
Context
Application Logic
EntityManager
EntityManagerFactory
Shared
Cache
Persistence
Context
Cache
Coordination
Cache Coordination
• Minimize stale entity caching across loosely coupled nodes.
• Database is always right
• Optimistic locking prevents corruption
• Fresh Entities retrieved from shared cache
• Stale Entities refreshed from database on access
• Creation and/or modification of Entity results in message to all
other nodes
• Shared cache size limited by heap of each node
• Cost of coordinating depends on messaging technology and
application
• cost of communication and processing may eventually exceed value of
caching
Introducing TopLink Grid
• 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.
• Supports 'JPA on the Grid' Architecture• EclipseLink JPA applications using Coherence as a shared
(L2) cache replacement along with configuration for more advanced usage
Oracle TopLink 11gR1
• Oracle‟s Enterprise Java Persistence Framework
• Includes open source EclipseLink with Commercial
Support
• Certified on WebLogic and redistributed by Oracle as part
of TopLink product and in WebLogic Server and Oracle
GlassFish
• TopLink Grid: JPA integration with Coherence
TopLink Grid with Coherence Cache
Application Logic
EntityManager
EntityManagerFactory
Persistence
Context
Application Logic
EntityManager
EntityManagerFactory
Persistence
Context
Coherence
Oracle Coherence Data GridDistributed in Memory Data Management
• Provides a reliable data
tier with a single,
consistent view of data
• Enables dynamic data capacity including fault tolerance and load balancing
• Ensures that data capacity scales withprocessing capacity
MainframesDatabases Web Services
Enterprise
Applications
Real Time
Clients
Web
Services
Oracle Coherence
Data Grid
Data Services
28
Coherence Topology Example
Distributed Topology
• Data spread and backed up across Members
• Transparent to developer
• Members have access to all Data
• All Data locations are known – no lookup & no registry!
29
Coherence Update Example
Distributed Topology
• Synchronous Update
• Avoids potential Data Loss & Corruption
• Predictable Performance
• Backup Partitions are partitioned away from Primaries for resilience
• No engineering requirement to setup Primaries or Backups
• Automatically and Dynamically Managed
30
Coherence Recovery Example
Distributed Topology
• No in-flight operations lost
• Some latencies (due to higher priority of recovery)
• Reliability, Availability, Scalability, Performance are the priority
• Degrade performance of some requests
JPAJPA
Coherence
JPA with Coherence
TX Commit
Application Logic
Cluster Node 1
Application Logic
Cluster Node N
ReadEntities
. . .
TopLink Grid—Configurations
• Grid Cache—Coherence as Shared (L2) Cache• Configurable per Entity type
• Entities read by one grid member are put into Coherence and are immediately available across the entire grid
• Grid Read• All supported read queries executed in the Coherence data
grid
• All writes performed directly on the database by TopLink (synchronously) and Coherence updated
• Grid Entity• All supported read queries and all writes are executed in the
Coherence data grid
JPA Application
Grid Cache ('Cache Aside')
• Reading:
• Primary Key queries check
Coherence first.
• If found in Coherence, Entity is
returned.
• If not found the database is
queried.
• Entities queried from database
are put() into Coherence and
returned to the application.
• Writing:
• All inserts, updates, and deletes
are directed to the database
• On successful commit,
Coherence is updated
Insert/Update/Delete
Query
PKQuery
Grid Cache—Leveraging Cache
• Cache is used when processing database results
• EclipseLink extracts primary keys from results and
checks cache to avoid object construction.
• Even if a SQL query is executed, an object cache can
still improve application throughput by eliminating
object construction costs for cached Entities
Grid Read
• All writes performed directly on database.
• Primary key queries result in get() on Coherence
• JPQL queries, e.g.,Select e from Employee E
are translated to Filters and executed in Coherence
• CacheLoaders should be configured to query database with PK query on cache miss
JPA Application
Query
PKQuery
Insert/Update/Delete
Coherence
Application Logic
EntityManager
EntityManagerFactory
Persistence
Context
TopLink Grid
Coherence Cache
Coherence Cache
Application Logic
EntityManager
EntityManagerFactory
Persistence
Context
Coherence Coherence
TopLink Grid
How is TopLink Grid different from
Hibernate with Coherence?
• Hibernate does not cache objects, it caches data rows
in Coherence
• Using Coherence as a cache for Hibernate
• Every cache hit incurs both object construction and
serialization costs
• Worse, object construction cost is paid by every cluster
member for every cache hit
• Hibernate only uses Coherence as a cache—TopLink
Grid is unique in supporting execution of queries
against Coherence which can significantly offload the
database and increase throughput
Summary
• EclipseLink supports a unique range of strategies for
scaling JPA applications
• TopLink Grid provides:
• An easy way for JPA developers to scale out their Java
EE applications
• 'JPA on the Grid' functionality to support scaling JPA
applications with Coherence
• Support for caching Entities with relationships in Coherence
Q U E S T I O N S
A N S W E R S