SlideShare a Scribd company logo
June 10-11, 2008 Berlin, Germany
Enterprise Persistence in OSGi
Mike Keith
Oracle Corporation
2
• Java and Persistence architect @ Oracle
• Have managed to stay on the server side for
most of my professional life
• Specialist in areas of distributed systems,
transactions and persistence
• Work with numerous expert groups and
committees (JCP, OSOA, OASIS, OSGi)
• EclipseLink project committer and mentor
Who Am I?
3
• Goals
• Background – EclipseLink and JPA
• Use Cases
• Problems Encountered
• Solutions (adopted or otherwise)
• Summary
Agenda
4
• Make EclipseLink OSGi-enabled
• JPA implementation working in OSGi
• Other EclipseLink components as well (JAXB, SDO)
• Agnostic to particular OSGi implementation (portable)
• Allow OSGi-based applications to use JPA
• Core application code should not require modification
in order to run in OSGi
• Applications should be able to leverage OSGi model
• Establish patterns and services for general use
• Work with others to come up with consensus path
• Feed results back into specification
Goals
5
EclipseLink Project
• Open source project at Eclipse
• Formally called Eclipse Persistence Services
• Fully featured open source evolution of TopLink
• Runtime project, not coupled to IDE
• Implements standards across all aspects of persistence:
• JPA - Relational databases,
• JAXB, SDO – XML schemas
• JCA – EIS and other types of non-relational stores
• Also offers XVA (extreme value-add) in all areas
• One-stop shop for persistence needs of any application
Runtime,
not tooling!
6
JDBC
Eclipse Persistence Services (EclipseLink)
Relational Databases
JPA MOXy SDO DBWS EIS
Java SE, Java EE, SCA, Spring, OSGi
Legacy SystemsXML Packaged Apps
JCA
7
EclipseLink Project
• Includes Eclipse JPA
• Fully compliant JPA implementation
• JPA 2.0 Reference Implementation (RI)
• Plus a plethora of extra features
• Cache coordination for clustered apps
• Deluxe customizable mappings
• Built-in support for Oracle database features
• Full Java expression-based query language
• Complete stored procedure support
• Many performance tuning enhancements
8
JPA On a Slide
• Application creates persistent domain objects
called “entities” which map to rows in database
• Simple configuration file called persistence.xml
• EntityManager API to control persistence and life
cycle of the entities
• Runs in managed or non-managed environments
• Container offers a number of ease of use services
• Underlying persistence provider offers the
implementation of the specification
9
Pluggable Persistence
package javax.persistence.spi;
public interface PersistenceProvider {
public EntityManagerFactory
createEntityManagerFactory(
String puName, Map map);
public EntityManagerFactory
createContainerEntityManagerFactory (
PersistenceUnitInfo info, Map map);
}
10
Container Mode
JPA can be used in two different modes
1. Container mode:
@Stateless
public class MySessionBean {
@PersistenceContext EntityManager em;
public Employee getEmployee(int empId) {
return em.find(Employee.class, empId);
}
...
}
11
Persistence Info
package javax.persistence.spi;
public interface PersistenceUnitInfo {
…
public DataSource getJtaDataSource();
public DataSource getNonJtaDataSource();
public List<String> getMappingFileNames();
public List<URL> getJarFileUrls();
public URL getPersistenceUnitRootUrl();
public List<String> getManagedClassNames();
public Properties getProperties();
public ClassLoader getClassLoader();
public void addTransformer(ClassTransformer transformer);
public ClassLoader getNewTempClassLoader();
}
12
Non-container Mode
2. Java SE mode:
public class DisplayEmployeeInfo {
public static void main(String[] args) {
EntityManagerFactory emf = Persistence
.createEntityManagerFactory(“PUnit”);
EntityManager em = emf.createEntityManager();
Employee emp = em.find(Employee.class,
new Integer(args[0]));
System.out.println(“EmployeeSummary: “ + emp);
em.close();
emf.close();
}
}
13
JPA – The Big Picture
Application persistence.jar
(javax.persistence.*)
META-INF/
persistence.xml
(Resource)
Domain
Model
(Entities)
RDB
JDBC
Eclipse JPA
(Persistence Provider JAR)
14
• Create a service that an application can look up and use to
obtain an EntityManagerFactory
Intuitive Solution
Application
JPA
javax.persistence.*
Eclipse JPA
JPA Implementation
META-INF/
persistence.xml
Domain Entities
Application Logic
Service Registry
Register
Look up
Service
org.osgi.sevice.jpa
OSGi
Persistence
15
• Need to create yet another interface that does
exactly what the existing Persistence class does
• Requires applications to use OSGi-specific service
lookups to obtain JPA artifacts
• More work to bring applications from other envs
• Not portable across environments
• Forces application to write code to choose specific
JPA provider (or not)
• If JPA provider dependency exists then it is already
declared within persistence.xml file => duplication
Problems with the Intuitive Solution
16
Use Cases
• Application bundle + JPA spec bundle + JPA
provider bundle(s)
Application
JPA
javax.persistence.*
Eclipse JPA
JPA Implementation
META-INF/
persistence.xml
Domain Entities
JDBC
Application Logic
JPA Provider
17
Use Cases
• Application bundle + Shared persistence unit
bundle + JPA bundle + JPA provider bundle(s)
Application
JPA
javax.persistence.*
Eclipse JPA
JPA Implementation
META-INF/
persistence.xml
Domain Entities
JDBC
Application 1 Logic
Application 2 Logic
18
• The JPA Provider needs to access resources
(persistence.xml and/or other XML files)
• JPA Provider needs to load and introspect domain
classes/entities
• List of classes is determined dynamically (by
contents of persistence.xml file
• Classes/resources may be in application bundle,
or separate bundle the application depends upon
• Application code calls out to JPA, but not
necessarily as part of initialization code
Problem: Upward Dependencies
19
“Buddy Loader” Strategy
• Equinox has “buddy loader” to allow bundles to
see up into bundles that depend on it
• Dependent bundle must declare the set of
bundles that are its “buddies”
• The application does not usually depend on the
underlying provider, only the API
• Not standard, so not portable
20
Solution: Standard BundleListener Trick
• Introduce a new bundle header:
“JPA-PersistenceUnits”
• Applications must add this to specify they are a
client of one or more persistence units
• Eclipse JPA registers a BundleListener and
keeps track of bundles that start up
• When a JPA client bundle starts, we remember
the persistence unit it uses and its classloader
• When client invokes JPA then we use the
classloader registered against that client
21
Problem: The JPA Specification
• javax.persistence.Persistence is a concrete
implementation class in the spec
• Makes use of a service provider pattern
documented in JAR specification
• Dynamically looks up service implementations
as resources in META-INF/services directory
• Uses context classloader for resource lookup
• Persistence class shipped in JPA spec jar
(persistence.jar)
22
Service Provider Pattern
JPA_Impl_1.jar
. . .
JVM Classpath
javax.persistence.Persistence
persistence.jar
Application
com.foo.Provider1
JPA_Impl_2.jar
com.bar.Provider2
META-INF/services META-INF/services
com.foo.Provider1
javax.persistence.
spi.Persistence.
Provider
com.bar.Provider2
javax.persistence.
spi.Persistence.
Provider
Persistence.
createEntitymanagerFactory()
getResources() getResources()
loadClass()
23
Solution: JPA Provider Service
• Use OSGi service model to allow providers to
register themselves as a JPA Provider service
• Allows providers to be dynamically installed
and removed
• Pluggable strategy for Persistence class to look
up installed JPA provider services
• Persistence class calls out to JPA Provider
services to resolve provider
• Hard dependency on a specific JPA Provider
averts lookup
24
Solution: JPA Provider Service
Application
JPA
javax.persistence.*
Eclipse JPA
JPA Implementation
META-INF/
persistence.xml
Domain Entities
Application Logic
Service Registry
Register
(Property = Provider class)
Look up
Service
javax.persistence.spi.
PersistenceProvider
25
• Many providers (including Eclipse JPA) employ
dynamic weaving techniques
• Lazy loading, change tracking, serialization issues
• Normally make use of SPI hook in Container
env, or Instrumentation (javaagent) in SE env
• No portable way to achieve this in OSGi environment
• Application code likely causes domain classes to
be loaded before calling out to JPA
• JPA provider “arrives too late to the party”
Problem: Weaving Domain Classes
26
• Equinox “adapter hooks” provide a similar ability
• Can install an adapter class into the system bundle
• Will get a callback when classes gets loaded
• Persistence units register a weaving service to indicate
they want a chance to weave
• Adapter class will find the service and pass newly loaded classes
to the weaving service to be woven
• Tightly coupled to Equinox
• Looking at complements in other implementations
• Similar idea to what AspectJ project uses (thanks guys!)
Solution: Use Adapter Hooks
27
• JDBC driver(s) must be deployed somewhere
• Client specifies the driver in its persistence
metadata (persistence.xml)
• It is the JPA Provider, not the client, that actually
loads and invokes the JDBC classes
• JPA Provider doesn’t know a priori the driver or
data source associated with a persistence unit
• In other environments JDBC driver is usually just
available in shared server layer (JAR in lib dir)
Problem: JDBC Placement
28
JDBC
Provider Extension Bundle
• Add as an extension to the boot classpath
• Refresh requires a framework restart
• Not widely supported anyway
Application JPA
javax.persistence.*
Eclipse JPA
JPA Implementation
META-INF/
persistence.xml
Domain Entities
Application 1 Logic
Extension
Bundle
System
Bundle
29
Provider Fragment Bundle
• Added as a fragment bundle to the provider
• Can’t change JDBC version without refreshing provider
Application
JPA
javax.persistence.*
Eclipse JPA
JPA Implementation
META-INF/
persistence.xml
Domain Entities
JDBC
Application 1 Logic
Fragment
Bundle
30
• Persistence.createEntityManagerFactory() facilitates
passing an argument Map of props
• May pass objects (not just String properties)
• Create a data source property that passes the JDBC
DataSource to the JPA Provider
• Need to modify the client to obtain and pass the data
source when obtaining the EntityManagerFactory
Map props = new HashMap();
props.put(DATA_SOURCE_PROPERTY, myDataSource);
EntityManagerFactory emf = Persistence
.createEntityManagerFactory(“myPUnit”, props);
DataSource Property
31
JDBC Bundled with the Client
• Bundled as part of the client or as a dependent bundle
• Accessed by the provider by virtue of its having the
client classloader
Application
JPA
javax.persistence.*
Eclipse JPA
JPA Implementation
META-INF/
persistence.xml
Domain Entities
JDBC
Application Logic
32
JDBC as a Client Dependent Bundle
• Can independently deploy or upgrade JDBC driver
• Accessed by the provider by virtue of its having the
client classloader
Application
JPA
javax.persistence.*
Eclipse JPA
JPA Implementation
META-INF/
persistence.xml
Domain Entities
Application Logic
JDBC
JDBC Driver
33
• Install as a bundle and register as a service
• Export a DataSourceFactory API that Provider
can look up based on driver type
• Achieves bundle independence with a service-
based access policy
• Solution designed to be easily extended for
other drivers
• Some standardization of properties is needed
• Generalized solution for JDBC access
Solution: JDBC Service
34
JDBC Service
Application
JPA
javax.persistence.*
Eclipse JPA
JPA Implementation
META-INF/
persistence.xml
Domain Entities
Application Logic
JDBC
JDBC Driver
Service Registry
Register
Look up
Service
DataSourceFactory createDataSource()
35
• Are the issues we encountered integrating JPA
with OSGi typical? experiences with other OSGi
services?
• Are there fundamental problems with OSGi
when it comes to implementing “real” services?
• Are there fundamental problems with JPA that
make it less service-based and more
application-bound in nature?
• Does JPA need to evolve, or OSGi, or both?
Introspection and Questions
36
• Look at how hard it would be to support a
managed (Container) API
• Injection, JTA transactions, auto-entity detection, etc.
• Come up with a better classloader strategy?
• Should an app need to indicate the persistence units
it uses in its manifest?
• Sort out the weaving story
• Fix JPA spec to accommodate different provider
resolution strategies
Future
37
• No standard Persistence service exists, even
though persistence is critical to every application
• Are still some general issues that we need to
resolve within the context of the specification
• We can work around some of the problems, but
can’t be implementation-independent right now
• Are anxious to help reify how Persistence
services work within OSGi
• Are open to suggestions and offers of assistance
and participation from interested parties
Summary

More Related Content

PDF
Java 8 in Anger (QCon London)
PDF
EclipseLink JPA
PDF
AAI 2235-OpenJPA and EclipseLink Usage Scenarios Explained
PDF
InterConnect 2016, OpenJPA and EclipseLink Usage Scenarios (PEJ-5303)
PDF
Haj 4308-open jpa, eclipselink, and the migration toolkit
PDF
Contributors Guide to the Jakarta EE 10 Galaxy
PPTX
Java 9 Module System Introduction
PPTX
The Eclipse Transformer Project
Java 8 in Anger (QCon London)
EclipseLink JPA
AAI 2235-OpenJPA and EclipseLink Usage Scenarios Explained
InterConnect 2016, OpenJPA and EclipseLink Usage Scenarios (PEJ-5303)
Haj 4308-open jpa, eclipselink, and the migration toolkit
Contributors Guide to the Jakarta EE 10 Galaxy
Java 9 Module System Introduction
The Eclipse Transformer Project

What's hot (20)

PPT
What's New in WebLogic 12.1.3 and Beyond
PDF
Silicon Valley JUG meetup July 18, 2018
PDF
Lecture 1: Introduction to JEE
PDF
Java 8 in Anger, Devoxx France
PPTX
Java EE 8
PPTX
JavaFX 2 Using the Spring Framework
PDF
AAI 2236-Using the New Java Concurrency Utilities with IBM WebSphere
PDF
Utilizing JSF Front Ends with Microservices
PDF
Build Cloud Applications with Akka and Heroku
PDF
Java 2012 conference keynote - Java Strategy & Roadmap - WebLogic & GlassFish...
PPTX
Advance java1.1
PDF
Java EE 8 Recipes
PDF
Oracle History #5
PDF
Orcale Presentation
PDF
DataFX - JavaOne 2013
PPT
Servlet programming
PPTX
Top 50 java ee 7 best practices [con5669]
PPT
Developing modular Java applications
PDF
Java EE Revisits GoF Design Patterns
What's New in WebLogic 12.1.3 and Beyond
Silicon Valley JUG meetup July 18, 2018
Lecture 1: Introduction to JEE
Java 8 in Anger, Devoxx France
Java EE 8
JavaFX 2 Using the Spring Framework
AAI 2236-Using the New Java Concurrency Utilities with IBM WebSphere
Utilizing JSF Front Ends with Microservices
Build Cloud Applications with Akka and Heroku
Java 2012 conference keynote - Java Strategy & Roadmap - WebLogic & GlassFish...
Advance java1.1
Java EE 8 Recipes
Oracle History #5
Orcale Presentation
DataFX - JavaOne 2013
Servlet programming
Top 50 java ee 7 best practices [con5669]
Developing modular Java applications
Java EE Revisits GoF Design Patterns
Ad

Similar to Enterprise Persistence in OSGi - Mike Keith, Oracle (20)

PPT
OSGi Persistence With EclipseLink
PPTX
Introduction to JPA (JPA version 2.0)
PPT
Al rihieli persistence
PPT
Jpa basics
PPT
PPT
ORM Concepts and JPA 2.0 Specifications
PPT
test for jpa spring boot persistence hehe
PDF
Java Persistence API
PDF
Java persistence api 2.1
PPTX
WebLogic Developer Webcast 1: JPA 2.0
PPT
Java Persistence API (JPA) - A Brief Overview
PPT
Java persistence api
PPTX
Jpa 2.1 Application Development
PDF
Lecture 9 - Java Persistence, JPA 2
PPTX
Jakarta Persistence (JPA) - Web Technologies
PPT
RESTful Data Access Services with Java EE
PPT
RESTful services with JAXB and JPA
PDF
AAI-2235 Open JPA and EclipseLink Usage Scenarios Explained
PDF
Java Persistence 2.0
PPT
Entity Persistence with JPA
OSGi Persistence With EclipseLink
Introduction to JPA (JPA version 2.0)
Al rihieli persistence
Jpa basics
ORM Concepts and JPA 2.0 Specifications
test for jpa spring boot persistence hehe
Java Persistence API
Java persistence api 2.1
WebLogic Developer Webcast 1: JPA 2.0
Java Persistence API (JPA) - A Brief Overview
Java persistence api
Jpa 2.1 Application Development
Lecture 9 - Java Persistence, JPA 2
Jakarta Persistence (JPA) - Web Technologies
RESTful Data Access Services with Java EE
RESTful services with JAXB and JPA
AAI-2235 Open JPA and EclipseLink Usage Scenarios Explained
Java Persistence 2.0
Entity Persistence with JPA
Ad

More from mfrancis (20)

PDF
Eclipse Modeling Framework and plain OSGi the easy way - Mark Hoffman (Data I...
PDF
OSGi and Java 9+ - BJ Hargrave (IBM)
PDF
Simplify Web UX Coding using OSGi Modularity Magic - Paul Fraser (A2Z Living)
PDF
OSGi for the data centre - Connecting OSGi to Kubernetes - Frank Lyaruu
PDF
Remote Management and Monitoring of Distributed OSGi Applications - Tim Verbe...
PDF
OSGi with Docker - a powerful way to develop Java systems - Udo Hafermann (So...
PDF
A real world use case with OSGi R7 - Jurgen Albert (Data In Motion Consulting...
PDF
OSGi Feature Model - Where Art Thou - David Bosschaert (Adobe)
PDF
Migrating from PDE to Bndtools in Practice - Amit Kumar Mondal (Deutsche Tele...
PDF
OSGi CDI Integration Specification - Ray Augé (Liferay)
PDF
How OSGi drives cross-sector energy management - Jörn Tümmler (SMA Solar Tech...
PDF
Improved developer productivity thanks to Maven and OSGi - Lukasz Dywicki (Co...
PDF
It Was Twenty Years Ago Today - Building an OSGi based Smart Home System - Ch...
PDF
Popular patterns revisited on OSGi - Christian Schneider (Adobe)
PDF
Integrating SLF4J and the new OSGi LogService 1.4 - BJ Hargrave (IBM)
PDF
OSG(a)i: because AI needs a runtime - Tim Verbelen (imec)
PDF
Flying to Jupiter with OSGi - Tony Walsh (ESA) & Hristo Indzhov (Telespazio V...
PDF
MicroProfile, OSGi was meant for this - Ray Auge (Liferay)
PDF
Prototyping IoT systems with a hybrid OSGi & Node-RED platform - Bruce Jackso...
PDF
How to connect your OSGi application - Dirk Fauth (Bosch)
Eclipse Modeling Framework and plain OSGi the easy way - Mark Hoffman (Data I...
OSGi and Java 9+ - BJ Hargrave (IBM)
Simplify Web UX Coding using OSGi Modularity Magic - Paul Fraser (A2Z Living)
OSGi for the data centre - Connecting OSGi to Kubernetes - Frank Lyaruu
Remote Management and Monitoring of Distributed OSGi Applications - Tim Verbe...
OSGi with Docker - a powerful way to develop Java systems - Udo Hafermann (So...
A real world use case with OSGi R7 - Jurgen Albert (Data In Motion Consulting...
OSGi Feature Model - Where Art Thou - David Bosschaert (Adobe)
Migrating from PDE to Bndtools in Practice - Amit Kumar Mondal (Deutsche Tele...
OSGi CDI Integration Specification - Ray Augé (Liferay)
How OSGi drives cross-sector energy management - Jörn Tümmler (SMA Solar Tech...
Improved developer productivity thanks to Maven and OSGi - Lukasz Dywicki (Co...
It Was Twenty Years Ago Today - Building an OSGi based Smart Home System - Ch...
Popular patterns revisited on OSGi - Christian Schneider (Adobe)
Integrating SLF4J and the new OSGi LogService 1.4 - BJ Hargrave (IBM)
OSG(a)i: because AI needs a runtime - Tim Verbelen (imec)
Flying to Jupiter with OSGi - Tony Walsh (ESA) & Hristo Indzhov (Telespazio V...
MicroProfile, OSGi was meant for this - Ray Auge (Liferay)
Prototyping IoT systems with a hybrid OSGi & Node-RED platform - Bruce Jackso...
How to connect your OSGi application - Dirk Fauth (Bosch)

Recently uploaded (20)

PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Approach and Philosophy of On baking technology
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Electronic commerce courselecture one. Pdf
PDF
Machine learning based COVID-19 study performance prediction
PDF
cuic standard and advanced reporting.pdf
PPTX
Big Data Technologies - Introduction.pptx
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Chapter 3 Spatial Domain Image Processing.pdf
Unlocking AI with Model Context Protocol (MCP)
Spectral efficient network and resource selection model in 5G networks
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Dropbox Q2 2025 Financial Results & Investor Presentation
Advanced methodologies resolving dimensionality complications for autism neur...
Review of recent advances in non-invasive hemoglobin estimation
20250228 LYD VKU AI Blended-Learning.pptx
Approach and Philosophy of On baking technology
Reach Out and Touch Someone: Haptics and Empathic Computing
MIND Revenue Release Quarter 2 2025 Press Release
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
“AI and Expert System Decision Support & Business Intelligence Systems”
Electronic commerce courselecture one. Pdf
Machine learning based COVID-19 study performance prediction
cuic standard and advanced reporting.pdf
Big Data Technologies - Introduction.pptx
Mobile App Security Testing_ A Comprehensive Guide.pdf
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Chapter 3 Spatial Domain Image Processing.pdf

Enterprise Persistence in OSGi - Mike Keith, Oracle

  • 1. June 10-11, 2008 Berlin, Germany Enterprise Persistence in OSGi Mike Keith Oracle Corporation
  • 2. 2 • Java and Persistence architect @ Oracle • Have managed to stay on the server side for most of my professional life • Specialist in areas of distributed systems, transactions and persistence • Work with numerous expert groups and committees (JCP, OSOA, OASIS, OSGi) • EclipseLink project committer and mentor Who Am I?
  • 3. 3 • Goals • Background – EclipseLink and JPA • Use Cases • Problems Encountered • Solutions (adopted or otherwise) • Summary Agenda
  • 4. 4 • Make EclipseLink OSGi-enabled • JPA implementation working in OSGi • Other EclipseLink components as well (JAXB, SDO) • Agnostic to particular OSGi implementation (portable) • Allow OSGi-based applications to use JPA • Core application code should not require modification in order to run in OSGi • Applications should be able to leverage OSGi model • Establish patterns and services for general use • Work with others to come up with consensus path • Feed results back into specification Goals
  • 5. 5 EclipseLink Project • Open source project at Eclipse • Formally called Eclipse Persistence Services • Fully featured open source evolution of TopLink • Runtime project, not coupled to IDE • Implements standards across all aspects of persistence: • JPA - Relational databases, • JAXB, SDO – XML schemas • JCA – EIS and other types of non-relational stores • Also offers XVA (extreme value-add) in all areas • One-stop shop for persistence needs of any application Runtime, not tooling!
  • 6. 6 JDBC Eclipse Persistence Services (EclipseLink) Relational Databases JPA MOXy SDO DBWS EIS Java SE, Java EE, SCA, Spring, OSGi Legacy SystemsXML Packaged Apps JCA
  • 7. 7 EclipseLink Project • Includes Eclipse JPA • Fully compliant JPA implementation • JPA 2.0 Reference Implementation (RI) • Plus a plethora of extra features • Cache coordination for clustered apps • Deluxe customizable mappings • Built-in support for Oracle database features • Full Java expression-based query language • Complete stored procedure support • Many performance tuning enhancements
  • 8. 8 JPA On a Slide • Application creates persistent domain objects called “entities” which map to rows in database • Simple configuration file called persistence.xml • EntityManager API to control persistence and life cycle of the entities • Runs in managed or non-managed environments • Container offers a number of ease of use services • Underlying persistence provider offers the implementation of the specification
  • 9. 9 Pluggable Persistence package javax.persistence.spi; public interface PersistenceProvider { public EntityManagerFactory createEntityManagerFactory( String puName, Map map); public EntityManagerFactory createContainerEntityManagerFactory ( PersistenceUnitInfo info, Map map); }
  • 10. 10 Container Mode JPA can be used in two different modes 1. Container mode: @Stateless public class MySessionBean { @PersistenceContext EntityManager em; public Employee getEmployee(int empId) { return em.find(Employee.class, empId); } ... }
  • 11. 11 Persistence Info package javax.persistence.spi; public interface PersistenceUnitInfo { … public DataSource getJtaDataSource(); public DataSource getNonJtaDataSource(); public List<String> getMappingFileNames(); public List<URL> getJarFileUrls(); public URL getPersistenceUnitRootUrl(); public List<String> getManagedClassNames(); public Properties getProperties(); public ClassLoader getClassLoader(); public void addTransformer(ClassTransformer transformer); public ClassLoader getNewTempClassLoader(); }
  • 12. 12 Non-container Mode 2. Java SE mode: public class DisplayEmployeeInfo { public static void main(String[] args) { EntityManagerFactory emf = Persistence .createEntityManagerFactory(“PUnit”); EntityManager em = emf.createEntityManager(); Employee emp = em.find(Employee.class, new Integer(args[0])); System.out.println(“EmployeeSummary: “ + emp); em.close(); emf.close(); } }
  • 13. 13 JPA – The Big Picture Application persistence.jar (javax.persistence.*) META-INF/ persistence.xml (Resource) Domain Model (Entities) RDB JDBC Eclipse JPA (Persistence Provider JAR)
  • 14. 14 • Create a service that an application can look up and use to obtain an EntityManagerFactory Intuitive Solution Application JPA javax.persistence.* Eclipse JPA JPA Implementation META-INF/ persistence.xml Domain Entities Application Logic Service Registry Register Look up Service org.osgi.sevice.jpa OSGi Persistence
  • 15. 15 • Need to create yet another interface that does exactly what the existing Persistence class does • Requires applications to use OSGi-specific service lookups to obtain JPA artifacts • More work to bring applications from other envs • Not portable across environments • Forces application to write code to choose specific JPA provider (or not) • If JPA provider dependency exists then it is already declared within persistence.xml file => duplication Problems with the Intuitive Solution
  • 16. 16 Use Cases • Application bundle + JPA spec bundle + JPA provider bundle(s) Application JPA javax.persistence.* Eclipse JPA JPA Implementation META-INF/ persistence.xml Domain Entities JDBC Application Logic JPA Provider
  • 17. 17 Use Cases • Application bundle + Shared persistence unit bundle + JPA bundle + JPA provider bundle(s) Application JPA javax.persistence.* Eclipse JPA JPA Implementation META-INF/ persistence.xml Domain Entities JDBC Application 1 Logic Application 2 Logic
  • 18. 18 • The JPA Provider needs to access resources (persistence.xml and/or other XML files) • JPA Provider needs to load and introspect domain classes/entities • List of classes is determined dynamically (by contents of persistence.xml file • Classes/resources may be in application bundle, or separate bundle the application depends upon • Application code calls out to JPA, but not necessarily as part of initialization code Problem: Upward Dependencies
  • 19. 19 “Buddy Loader” Strategy • Equinox has “buddy loader” to allow bundles to see up into bundles that depend on it • Dependent bundle must declare the set of bundles that are its “buddies” • The application does not usually depend on the underlying provider, only the API • Not standard, so not portable
  • 20. 20 Solution: Standard BundleListener Trick • Introduce a new bundle header: “JPA-PersistenceUnits” • Applications must add this to specify they are a client of one or more persistence units • Eclipse JPA registers a BundleListener and keeps track of bundles that start up • When a JPA client bundle starts, we remember the persistence unit it uses and its classloader • When client invokes JPA then we use the classloader registered against that client
  • 21. 21 Problem: The JPA Specification • javax.persistence.Persistence is a concrete implementation class in the spec • Makes use of a service provider pattern documented in JAR specification • Dynamically looks up service implementations as resources in META-INF/services directory • Uses context classloader for resource lookup • Persistence class shipped in JPA spec jar (persistence.jar)
  • 22. 22 Service Provider Pattern JPA_Impl_1.jar . . . JVM Classpath javax.persistence.Persistence persistence.jar Application com.foo.Provider1 JPA_Impl_2.jar com.bar.Provider2 META-INF/services META-INF/services com.foo.Provider1 javax.persistence. spi.Persistence. Provider com.bar.Provider2 javax.persistence. spi.Persistence. Provider Persistence. createEntitymanagerFactory() getResources() getResources() loadClass()
  • 23. 23 Solution: JPA Provider Service • Use OSGi service model to allow providers to register themselves as a JPA Provider service • Allows providers to be dynamically installed and removed • Pluggable strategy for Persistence class to look up installed JPA provider services • Persistence class calls out to JPA Provider services to resolve provider • Hard dependency on a specific JPA Provider averts lookup
  • 24. 24 Solution: JPA Provider Service Application JPA javax.persistence.* Eclipse JPA JPA Implementation META-INF/ persistence.xml Domain Entities Application Logic Service Registry Register (Property = Provider class) Look up Service javax.persistence.spi. PersistenceProvider
  • 25. 25 • Many providers (including Eclipse JPA) employ dynamic weaving techniques • Lazy loading, change tracking, serialization issues • Normally make use of SPI hook in Container env, or Instrumentation (javaagent) in SE env • No portable way to achieve this in OSGi environment • Application code likely causes domain classes to be loaded before calling out to JPA • JPA provider “arrives too late to the party” Problem: Weaving Domain Classes
  • 26. 26 • Equinox “adapter hooks” provide a similar ability • Can install an adapter class into the system bundle • Will get a callback when classes gets loaded • Persistence units register a weaving service to indicate they want a chance to weave • Adapter class will find the service and pass newly loaded classes to the weaving service to be woven • Tightly coupled to Equinox • Looking at complements in other implementations • Similar idea to what AspectJ project uses (thanks guys!) Solution: Use Adapter Hooks
  • 27. 27 • JDBC driver(s) must be deployed somewhere • Client specifies the driver in its persistence metadata (persistence.xml) • It is the JPA Provider, not the client, that actually loads and invokes the JDBC classes • JPA Provider doesn’t know a priori the driver or data source associated with a persistence unit • In other environments JDBC driver is usually just available in shared server layer (JAR in lib dir) Problem: JDBC Placement
  • 28. 28 JDBC Provider Extension Bundle • Add as an extension to the boot classpath • Refresh requires a framework restart • Not widely supported anyway Application JPA javax.persistence.* Eclipse JPA JPA Implementation META-INF/ persistence.xml Domain Entities Application 1 Logic Extension Bundle System Bundle
  • 29. 29 Provider Fragment Bundle • Added as a fragment bundle to the provider • Can’t change JDBC version without refreshing provider Application JPA javax.persistence.* Eclipse JPA JPA Implementation META-INF/ persistence.xml Domain Entities JDBC Application 1 Logic Fragment Bundle
  • 30. 30 • Persistence.createEntityManagerFactory() facilitates passing an argument Map of props • May pass objects (not just String properties) • Create a data source property that passes the JDBC DataSource to the JPA Provider • Need to modify the client to obtain and pass the data source when obtaining the EntityManagerFactory Map props = new HashMap(); props.put(DATA_SOURCE_PROPERTY, myDataSource); EntityManagerFactory emf = Persistence .createEntityManagerFactory(“myPUnit”, props); DataSource Property
  • 31. 31 JDBC Bundled with the Client • Bundled as part of the client or as a dependent bundle • Accessed by the provider by virtue of its having the client classloader Application JPA javax.persistence.* Eclipse JPA JPA Implementation META-INF/ persistence.xml Domain Entities JDBC Application Logic
  • 32. 32 JDBC as a Client Dependent Bundle • Can independently deploy or upgrade JDBC driver • Accessed by the provider by virtue of its having the client classloader Application JPA javax.persistence.* Eclipse JPA JPA Implementation META-INF/ persistence.xml Domain Entities Application Logic JDBC JDBC Driver
  • 33. 33 • Install as a bundle and register as a service • Export a DataSourceFactory API that Provider can look up based on driver type • Achieves bundle independence with a service- based access policy • Solution designed to be easily extended for other drivers • Some standardization of properties is needed • Generalized solution for JDBC access Solution: JDBC Service
  • 34. 34 JDBC Service Application JPA javax.persistence.* Eclipse JPA JPA Implementation META-INF/ persistence.xml Domain Entities Application Logic JDBC JDBC Driver Service Registry Register Look up Service DataSourceFactory createDataSource()
  • 35. 35 • Are the issues we encountered integrating JPA with OSGi typical? experiences with other OSGi services? • Are there fundamental problems with OSGi when it comes to implementing “real” services? • Are there fundamental problems with JPA that make it less service-based and more application-bound in nature? • Does JPA need to evolve, or OSGi, or both? Introspection and Questions
  • 36. 36 • Look at how hard it would be to support a managed (Container) API • Injection, JTA transactions, auto-entity detection, etc. • Come up with a better classloader strategy? • Should an app need to indicate the persistence units it uses in its manifest? • Sort out the weaving story • Fix JPA spec to accommodate different provider resolution strategies Future
  • 37. 37 • No standard Persistence service exists, even though persistence is critical to every application • Are still some general issues that we need to resolve within the context of the specification • We can work around some of the problems, but can’t be implementation-independent right now • Are anxious to help reify how Persistence services work within OSGi • Are open to suggestions and offers of assistance and participation from interested parties Summary