Skip to main content
Jump to: navigation, search

EclipseLink/DesignDocs/328404 new

Design Specification: Persistence Unit Composition

This feature adds support for persistence units being composed. This means a user can define multiple persistence units with a unique set of entity types and expose them through a composite persistence unit which combines the set of classes across multiple data sources to expose a single persistence context.

ER 328404

Document History

[old version of this doc]

Date committer description
2011/04/06 Andrei Ilitchev Initial New Version

Project overview

The goal is to combine several persistence units into a single one.

em.persist(new A(..));
em.persist(new Z(..));
// insert A into db1; insert Z into db2:
// the two different data bases possibly from different vendors



  • Data Source: For the purposes of this discussion a data-source will include JTA and non-JTA data sources as well as EclipseLink's native JDBC connection pools.
  • Composite PU: Refers to a PU which combines 2 or more Member PUs to provide a persistence context which allows queries and transactions (with limitations) across the combined set of entity types.


# Description
1 Add support for a single persistence context accessing entities stored in different data sources (schemas/table-space/database) where application developers can perform queries and transactions across the complete set of entities transparently.
2 Support mapping relationships between entities in different data sources.
3 Provide easy to use JPA configuration.
4 Clearly capture usage limitations and exceptions expected in Java Docs and user documentation. The most basic example of this is the ability to join across tables in different data sources. This limitation effects mapping and query execution and optimizations. These issues need to be identified and documented.
5 Support container managed and application bootstrap JPA usage with dynamic, static, and no weaving.

User Bugs

The following potentially related bugs should be taken into consideration in this feature work.

  • JPA enhancement requests.
    • bug 260258 - Add JPA EntityManagerFactoryBroker support
  • Core bugs
    • bug 269213 - SessionBroker handling of ExceptionHandler broken because stale copy/paste
    • bug 281569 - Integration of JPA using transaction-type="JTA" but without EJB container, set-up incomplete commit cleanup
    • bug 326649 - DatabaseSessionImpl.finalize() leads to ClassCastException
    • bug 329185 - SessionBroker: ClassCastException in Cursor.buildAndRegisterObject between ServerSession and UnitOfWorkImpl
    • bug 332581 - SessionBroker session event issues to consider and resolve when implementing JPA Session Broker
    • bug 331712 - Allow Isolated classes to work with client session broker

Design Constraints

Design / Functionality




The Dali team should be made aware of this feature work and track an enhancement request to uptake it.

Config files

  • BrokerPU must be configured in persistence.xml using property:
* Indicates if it's a broker persistence unit ("true").
* The property must be specified in persistence.xml of a broker persistence unit.
* The property passed to createEntityManagerFactory method or in system properties is ignored.
* Broker persistence unit would contain all persistence units found in jar files specified by <jar-file> elements in persistence.xml. 
*    <jar-file>contained1.jar</jar-file>
*    <jar-file>contained2.jar</jar-file>
*    <properties>
*        <property name="" value="true"/>
*    </properties>
* Currently broker persistence unit cannot itself be contained in another broker persistence unit.
* Contained persistence units could be independent or contained-only.
public static final String IS_BROKER_PERSISTENCE_UNIT = "";
  • ContainedPU may specify in persistence.xml that it can't be used as an independent persistence unit:
* Indicates if the persistence unit must be contained in a broker persistence unit ("true"),
* can't be used as an independent persistence unit.
* That happens if contained persistence unit has dependencies on other contained persistence unit(s).
* The property may be specified in persistence.xml of contained persistence unit.
* The property passed to createEntityManagerFactory method or in system properties is ignored.
* Though EntityManagerFactory for contained-only persistence unit could be created,
* it can't be connected: an attempt to create entity manager would cause an exception.
public static final String IS_CONTAINED_PERSISTENCE_UNIT_ONLY = "";
  • While creating BrokerPU EntityManagerFactory properties could be passed to ContainedPUs:
* The property may be passed to createEntityManagerFactory method of a broker persistence unit
* to pass properties to contained persistence units.
* The value is a map: 
* the key is contained persistence unit's name,
* the value is a map of properties to be passed to this persistence unit. 
* "eclipselink.containes-persistence-units-properties" -> (
*   ("containedPu1" -> (   "javax.persistence.jdbc.user" -> "user1", 
*                          "javax.persistence.jdbc.password" -> "password1",
*                          "javax.persistence.jdbc.driver" -> "oracle.jdbc.OracleDriver",
*                          "javax.persistence.jdbc.url" -> "jdbc:oracle:thin:@oracle_db_url:1521:db",
*                      ) , 
*   ("containedPu2" -> (   "javax.persistence.jdbc.user" -> "user2",
*                          "javax.persistence.jdbc.password" -> "password2"
*                          "javax.persistence.jdbc.driver" -> "com.mysql.jdbc.Driver",
*                          "javax.persistence.jdbc.url" -> "jdbc:mysql://my_sql_db_url:3306/user2",
*                      )
* )
public static final String CONTAINED_PERSISTENCE_UNITS_PROPERTIES = "eclipselink.contained-persistence-units-properties";


  • brokerPu specifies transaction type and server platform. It will contain all persistence units defined in contained1.jar and contained2.jar files.
<persistence xmlns="" xmlns:xsi="" xsi:schemaLocation=" persistence_1_0.xsd" version="1.0">
    <persistence-unit name="brokerPu" transaction-type="JTA">
            <property name="" value="true"/>
            <property name="" value="WebLogic_10"/>
  • containedPu1 defined in contained1.jar file. It could be used independently as well as inside broker persistence unit.
<persistence xmlns="" xmlns:xsi="" xsi:schemaLocation=""<persistence xmlns="" xmlns:xsi="" xsi:schemaLocation=" persistence_1_0.xsd" version="1.0">
    <persistence-unit name="containedPu1" transaction-type="JTA">
            <property name="" value="WebLogic_10"/>
            <property name="" value=""/>
  • containedPu2 defined in contained2.jar file. It has dependency on a class defined in contained1.jar and can't be used independently.
<persistence xmlns="" xmlns:xsi="" xsi:schemaLocation="<persistence xmlns="" xmlns:xsi="" xsi:schemaLocation=" persistence_1_0.xsd" version="1.0">
    <persistence-unit name="containedPu2">
            <property name="" value="true"/>
            <property name="" value="org.eclipse.persistence.platform.database.MySQLPlatform"/>

All three jars - broker and two contained - should be deployed on the same class loader. In case of application server the jars should by packed in an ear file; in standalone case put on the class pass. As usual BrokerPU could be accessed either through injection (in app. server case):

EntityManagerFactory entityManagerFactory;
EntityManager entityManager;

or using Persistence:

EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("brokerPu", properties);


Cannot join tables from different data sources in a query.

Still to come

Properties usage: how brokerPu and containedPu properties are processed: which one takes precedence, what is ignored etc.


Open Issues

This section lists the open issues that are still pending that must be decided prior to fully implementing this project's requirements.

Issue # Description / Notes
1 Should this feature include both split and aggregated persistence units? NO split support.
2 In the case of split persistence units where should the additional data sources be defined? PU properties in persistence.xml, eclipselink-orm.xml, both? Since it is possible for different data sources to be different database vendors or versions it should be possible to configure all JDBC/connection/pool level options on each data source. NO split support.
3 Should this feature include support to customize additional datasources with persistence unit properties. NO.
4 In the case of aggregate persistence units, should persistence units be usable on their own. YES, if independent.


This section lists decisions made. These are intended to document the resolution of open issues or constraints added to the project that are important.

Issue # Description / Notes Decision

Back to the top