Top Banner
<Insert Picture Here> TopLink Grid: Scaling JPA applications with Coherence Andrei Cristian Niculae Technology Presales Consultant
40

Introduction to JPA and TopLink Grid

Sep 03, 2014

Download

Technology

 
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Introduction to JPA and TopLink Grid

<Insert Picture Here>

TopLink Grid: Scaling JPA applications with

Coherence

Andrei Cristian Niculae

Technology Presales Consultant

Page 2: Introduction to JPA and TopLink Grid

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

Page 3: Introduction to JPA and TopLink Grid

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

Page 4: Introduction to JPA and TopLink Grid

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

Page 5: Introduction to JPA and TopLink Grid

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.

Page 6: Introduction to JPA and TopLink Grid

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

Page 7: Introduction to JPA and TopLink Grid

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)

Page 8: Introduction to JPA and TopLink Grid

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

Page 9: Introduction to JPA and TopLink Grid

JPA Object-Relational Mappings

• Core JPA Mappings

• Id

• Basic

• Relationships

• OneToOne

• ManyToOne

• OneToMany

• ManyToMany

• And more…

• Annotations and/or XML

Page 10: Introduction to JPA and TopLink Grid

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;

}

}

Page 11: Introduction to JPA and TopLink Grid

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;

}

}

Page 12: Introduction to JPA and TopLink Grid

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>

Page 13: Introduction to JPA and TopLink Grid

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

Page 14: Introduction to JPA and TopLink Grid

Example JPA Client Code

Page 15: Introduction to JPA and TopLink Grid

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

Page 16: Introduction to JPA and TopLink Grid

JPA

JPA with Shared Cache

TX Commit

Application Logic

TX n

Application Logic

TX n+1

ReadEntities

Cache hits avoid object build cost

Page 17: Introduction to JPA and TopLink Grid

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. . .

Page 18: Introduction to JPA and TopLink Grid

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

Page 19: Introduction to JPA and TopLink Grid

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

Page 20: Introduction to JPA and TopLink Grid

Strategy 1: Disable Shared Cache

EntityManagerFactory EntityManagerFactory

Application Logic

EntityManager

Persistence

Context

Application Logic

EntityManager

Persistence

Context

Page 21: Introduction to JPA and TopLink Grid

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

Page 22: Introduction to JPA and TopLink Grid

Strategy 2: Cache Coordination

Application Logic

EntityManager

EntityManagerFactory

Shared

Cache

Persistence

Context

Application Logic

EntityManager

EntityManagerFactory

Shared

Cache

Persistence

Context

Cache

Coordination

Page 23: Introduction to JPA and TopLink Grid

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

Page 24: Introduction to JPA and TopLink Grid

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

Page 25: Introduction to JPA and TopLink Grid

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

Page 26: Introduction to JPA and TopLink Grid

TopLink Grid with Coherence Cache

Application Logic

EntityManager

EntityManagerFactory

Persistence

Context

Application Logic

EntityManager

EntityManagerFactory

Persistence

Context

Coherence

Page 27: Introduction to JPA and TopLink Grid

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

Page 28: Introduction to JPA and TopLink Grid

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!

Page 29: Introduction to JPA and TopLink Grid

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

Page 30: Introduction to JPA and TopLink Grid

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

Page 31: Introduction to JPA and TopLink Grid

JPAJPA

Coherence

JPA with Coherence

TX Commit

Application Logic

Cluster Node 1

Application Logic

Cluster Node N

ReadEntities

. . .

Page 32: Introduction to JPA and TopLink Grid

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

Page 33: Introduction to JPA and TopLink 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

Page 34: Introduction to JPA and TopLink Grid

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

Page 35: Introduction to JPA and TopLink Grid

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

Page 36: Introduction to JPA and TopLink Grid

Coherence

Application Logic

EntityManager

EntityManagerFactory

Persistence

Context

TopLink Grid

Coherence Cache

Page 37: Introduction to JPA and TopLink Grid

Coherence Cache

Application Logic

EntityManager

EntityManagerFactory

Persistence

Context

Coherence Coherence

TopLink Grid

Page 38: Introduction to JPA and 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

Page 39: Introduction to JPA and TopLink Grid

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

Page 40: Introduction to JPA and TopLink Grid

Q U E S T I O N S

A N S W E R S