Transcript
Page 1: OSGi JPA Spec and Gemini JPA

<Insert Picture Here>

OSGi and Gemini JPA

Page 2: OSGi JPA Spec and Gemini JPA

Java Persistence: The Problem Space

CUST

ID NAME C_RATING

Relational

Customerid: int

name: String

creditRating: int

Java

Java Persistence

API

Page 3: OSGi JPA Spec and Gemini JPA

Java Persistence API (JPA)

• Defines:– How Java objects are

stored in relational– A programmer API for

reading, writing, and querying persistent Java objects (“Entities”)

– A full featured query language in JP QL

Java Classes

Database Schema

Application

JDBC Driver

JPA Provider

PersistenceDescriptors

Java SE/EE

Page 4: OSGi JPA Spec and Gemini JPA

Mechanics of a JPA Application

ReadEntities

Step 1

Application Logic

JPA Provider

Create/ModifyEntities

Step 2Application Logic

JPA Provider JPA Provider`

Insert/UpdateEntities

Step 3Application Logic

JDBC Driver JDBC Driver JDBC Driver

Page 5: OSGi JPA Spec and Gemini JPA

Example JPA Client Code

Page 6: OSGi JPA Spec and Gemini JPA

A non-OSGi Java SE/EE JPA Application

JDBC Driver

JPA Provider

Application

JavaClasses

PersistenceDescriptors

Java SE/EE

Set of Jars with Flat Classloader Space

Page 7: OSGi JPA Spec and Gemini JPA

An OSGi JPA Application

ClientBundle

PersistenceBundle

JPA ProviderBundle

JDBC ProviderBundle

Entity Manager Factory Service

JDBC Data Source Service

Set of OSGi Bundles and Services

Page 8: OSGi JPA Spec and Gemini JPA

The OSGi Java Persistence Service Specification

• Part of the OSGi Enterprise Specification• Defines:

– How persistence units (Entities and metadata) can be published

– How client's can find persistence units– How database drivers are found using the OSGi JDBC spec– How JPA Providers can be made available in OSGi

Page 9: OSGi JPA Spec and Gemini JPA

Eclipse Enterprise Modules Project: 'Gemini'

• An open source project developing of implementations of the services defined in the OSGi Enterprise Specification including:• RFC 66 – Web Container•  RFC 122 – Database Access•  RFC 124 – Blueprint Service•  RFC 139 – JMX Integration•  RFC 142 – JNDI Integration•  RFC 143 – JPA Integration

• Subproject of Eclipse Runtime (RT) Project

Page 10: OSGi JPA Spec and Gemini JPA

EclipseLink Project

• Provides JPA, JAXB, SDO, DBWS, and EIS persistence services

• Open source Eclipse project• Founded by Oracle with the contribution of full

TopLink source code and tests• Based upon product with 12+ years of commercial

usage• Basis for Gemini OSGi JPA Service Implementation

Page 11: OSGi JPA Spec and Gemini JPA

• Entity classes• Persistence Descriptor declaration in manifest

– Always processes META-INF/persistence.xml if present– Supports multiple descriptors and descriptors in jars:Meta-Persistence: META-INF/accounting.xml, payroll.jar!/META-INF/payroll.xml

Persistence Bundle

PersistenceBundle

JPA Provider

JDBC Driver

Application

Java Classes

Page 12: OSGi JPA Spec and Gemini JPA

• Application Logic– Must import Entity classes or require Persistence Bundle that

exports Entity classes– Persistence Bundle may also be the Client Bundle—

application logic and Entities in same bundle

Client Bundle

ClientBundle

JPA Provider

JDBC Driver

Application

Java Classes

Page 13: OSGi JPA Spec and Gemini JPA

• JPA implementation providing a Persistence Provider and JPA Services

JPA Provider Bundle

JPA ProviderBundle

Application Logic

JDBC Driver

JPA Provider

Entity Manager Factory Service

Page 14: OSGi JPA Spec and Gemini JPA

JDBC Provider Bundle

• JDBC driver implementation providing Data Source service.

JDBC ProviderBundle

Application Logic

JPA Provider

JDBC Driver

JDBC Data Source Service

Page 15: OSGi JPA Spec and Gemini JPA

JPA OSGi Example

ClientBundle

PersistenceBundle

'Accounting' Entity Manager

Factory Service?

Page 16: OSGi JPA Spec and Gemini JPA

JPA OSGi Example

ClientBundle

PersistenceBundle

'Accounting' Entity Manager

Factory Service?

Page 17: OSGi JPA Spec and Gemini JPA

JPA OSGi Example

ClientBundle

PersistenceBundle

JPA ProviderBundle

JPA Provider Bundle Started

'Accounting' Entity Manager

Factory Service?

Page 18: OSGi JPA Spec and Gemini JPA

JPA OSGi Example

ClientBundle

PersistenceBundle

JPA ProviderBundle

Detect & Process Persistence Descriptor

'Accounting' Entity Manager

Factory Service?

Page 19: OSGi JPA Spec and Gemini JPA

JPA OSGi Example

ClientBundle

PersistenceBundle

JPA ProviderBundle

'Accounting' Entity Manager

Factory Service?

'Accounting' PU with required data

source declared

Page 20: OSGi JPA Spec and Gemini JPA

JPA OSGi Example

ClientBundle

PersistenceBundle

JPA ProviderBundle

'Accounting' Entity Manager

Factory Service?

JDBC Data Source for 'Accounting'

PU?

Page 21: OSGi JPA Spec and Gemini JPA

JPA OSGi Example

ClientBundle

PersistenceBundle

JPA ProviderBundle

'Accounting' Entity Manager

Factory Service?

JDBC Data Source for 'Accounting'

PU?

Page 22: OSGi JPA Spec and Gemini JPA

JPA OSGi Example

JDBC ProviderBundle

ClientBundle

PersistenceBundle

JPA ProviderBundle

'Accounting' Entity Manager

Factory Service?

JDBC Data Source for 'Accounting'

PU?

JDBC Driver Bundle Started

Page 23: OSGi JPA Spec and Gemini JPA

JPA OSGi Example

JDBC ProviderBundle

JDBC Data Source Service

ClientBundle

PersistenceBundle

JPA ProviderBundle

'Accounting' Entity Manager

Factory Service?

JDBC Data Source for 'Accounting'

PU?

Data Source Service

Published

Page 24: OSGi JPA Spec and Gemini JPA

JPA OSGi Example

JDBC ProviderBundle

JDBC Data Source Service

ClientBundle

PersistenceBundle

JPA ProviderBundle

'Accounting' Entity Manager

Factory Service?

JDBC Data Source for 'Accounting'

PU?

Page 25: OSGi JPA Spec and Gemini JPA

JPA OSGi Example

ClientBundle

PersistenceBundle

JPA ProviderBundle

'Accounting' Entity Manager

Factory Service?

JDBC ProviderBundle

JDBC Data Source Service

'Accounting' Entity Manager Factory Service

Entity Manager Factory Service Published

Page 26: OSGi JPA Spec and Gemini JPA

JPA OSGi Example

ClientBundle

PersistenceBundle

JPA ProviderBundle

'Accounting' Entity Manager

Factory Service?

JDBC ProviderBundle

JDBC Data Source Service

'Accounting' Entity Manager Factory Service

Page 27: OSGi JPA Spec and Gemini JPA

JPA OSGi Example

ClientBundle

PersistenceBundle

JPA ProviderBundle

JDBC ProviderBundle

JDBC Data Source Service

'Accounting' Entity Manager Factory Service

Obtain Entity Manager Factory from service

Page 28: OSGi JPA Spec and Gemini JPA

Entity Manager Factory Considerations

• Clients should track Entity Manager Factory service to respond to loss of service

• No need to track Data Source service as this is tracked by Entity Manager Factory service

Page 29: OSGi JPA Spec and Gemini JPA

Entity Manager Factory Builder Service

• If a persistence unit does not declare a data source:– Clients must supply data source connectivity information at

Entity Manager Factory creation time.– Clients cannot use Entity Manager Factory Service because it

will only publish for Persistence Units whose data source service is declared and available.

– Solution: JPA Providers also publish an Entity Manager Factory Builder service for each Persistence Unit that takes connectivity information

• Builder also for specifying properties at runtime

Page 30: OSGi JPA Spec and Gemini JPA

Entity Manager Factory Builder

PersistenceBundle

JPA ProviderBundle'Accounting'

Entity Manager Factory Builder

Service

Page 31: OSGi JPA Spec and Gemini JPA

Entity Manager Factory Builder

PersistenceBundle

JPA ProviderBundle

JDBC ProviderBundle

JDBC Data Source Service

'Accounting' Entity Manager Factory Service

'Accounting' Entity Manager Factory Builder

Service

Page 32: OSGi JPA Spec and Gemini JPA

Entity Manager Factory Builder Considerations

• If specified Data Source service is not available createEntityManager(..) call will return null.

• Clients should ensure Data Source service is available before trying to create an Entity Manager

• Clients should track both Data Source and Entity Manager Factory Builder services to respond to loss of service

Page 33: OSGi JPA Spec and Gemini JPA

Static Clients

• Previous slides show client using OSGi services to obtain JPA Entity Manager Factory

• Non-OSGi aware clients use static methods on JPA SPI class javax.persistence.Persistence, e.g.:EntityManagerFactory emf = Persistence.createEntityManager("Accounting");

• For compatibility OSGi JPA Service specification provides support for "Static Clients" that use JPA SPI.

Page 34: OSGi JPA Spec and Gemini JPA

Interface Bundle

• Exports JPA Spec packages javax.persistence.*– Packages imported by Persistence

Bundles, JPA Provider Bundles, and Client Bundles that use the JPA

• Provides static methods that wrap OSGi JPA services– Achieved using OSGi service aware

JPA Provider locator strategy

InterfaceBundle

Page 35: OSGi JPA Spec and Gemini JPA

JPA OSGi Static Client Example

ClientBundle

PersistenceBundle

InterfaceBundle

Persistence.createEntityManagerFactory("Accounting");

JPA ProviderBundle

JDBC ProviderBundle

JDBC Data Source Service

'Accounting' Entity Manager Factory Service

Page 36: OSGi JPA Spec and Gemini JPA

JPA OSGi Static Client Example

ClientBundle

PersistenceBundle

InterfaceBundle

JPA ProviderBundle

JDBC ProviderBundle

JDBC Data Source Service

'Accounting' Entity Manager Factory Service

'Accounting' Entity Manager

Factory Service?

Page 37: OSGi JPA Spec and Gemini JPA

JPA OSGi Static Client Example

ClientBundle

PersistenceBundle

InterfaceBundle

JPA ProviderBundle

JDBC ProviderBundle

JDBC Data Source Service

'Accounting' Entity Manager Factory Service

'Accounting' Entity Manager

Factory Service?

Page 38: OSGi JPA Spec and Gemini JPA

JPA OSGi Static Client Example

ClientBundle

PersistenceBundle

InterfaceBundle

Obtain Entity Manager Factory JPA Provider

Bundle

JDBC ProviderBundle

JDBC Data Source Service

'Accounting' Entity Manager Factory Service

Page 39: OSGi JPA Spec and Gemini JPA

Static Client Limitations

• Fully synchronous so if Entity Manager and Data Source services are not available createEntityManager() call will return null.– Dependent on bundle start order for success

• Client not informed when services become unavailable—can lead to unpredictable failures

• Not ideal in dynamic OSGi environment

Page 40: OSGi JPA Spec and Gemini JPA

Versioning

• JPA 1.0 vs. 2.0– JPA Provider selected using class consistency– Clients importing JPA API classes will be matched with JPA

Providers that import the same API classes• Persistence Unit Versions

– Specify osgi.unit.version attribute when locating Entity Manager Factory [Builder] service

– Entity Manager Factory [Builder] service's version is Persistence Bundle's version

• JPA Provider– No standard way to specify a provider version, e.g.

EclipseLink 1.2 vs. 2.0

Page 41: OSGi JPA Spec and Gemini JPA

Byte Code Weaving

• Byte Code Weaving is used by some JPA Providers to implement lazy loading, change tracking, and other performance features.• The OSGi JPA Service specification does not define how

weaving may be supported. Implementation details are left to providers.

Page 42: OSGi JPA Spec and Gemini JPA

Summary

• The Java Persistence API is the Java standard for object-relational mapping

• The OSGi JPA Service Specification, a part of the OSGi Enterprise Specification, is the new standard for JPA usage in OSGi

• The Eclipse Gemini project is delivering the OSGi JPA Service specification reference implementation based on EclipseLink, the JPA 2.0 reference implementation

• A standard and portable way to use JPA is now available for OSGi!


Top Related