Configuring two resource reference files on the same data source

You can configure two resource reference files on the same data source. This allows you to extend the custom properties for the data source to be extended to include two different schema names (currentSQLId on z/OS® or currentSchema name in the custom properties) that can be used to exploit the capabilities of the application server.

About this task

When an EntityManager is created, the application server obtains a connection to the database. When you are using a pessimistic transaction, the EntityManager will retain that connection until the EntityManager is closed. When there are two EntityManagers that extend the data source definitions, the openjpa.jdbc.TransactionIsolation property might cause a problem with the transaction. This property can be found in the persistence.xml file in the following entry:
property name="openjpa.jdbc.TransactionIsolation" value="read-committed" 
In order to satisfy this request, Java™ Persistence API (JPA) will obtain a connection and immediately call setTransactionIsolation(READ_COMMITTED). When you have two EntityManagers share a single physical connection to the database, the first EntityManager creates a connection to the database and involves that connection in a transaction. When the second EntityManager creates a connection, it is not able to change the isolation level.
You can avoid this problem by creating two resource reference files in the same data source. You can create the resource references with Rational® Application Developer or by editing the XML files. You will need to make changes to the ejb-jar.xml, ibm-ejb-jar-bnd.xml, ibm-ejb-jar-ext.xml, persistence.xml files.
Note: For IBM® Optim™ PureQuery Runtime, if this is an XA data source you must define a new custom property on the data source where property_name = downgradeHoldCursorsUnderXa and boolean value = true.
See the following sections for information on how to accomplish this:

Procedure

  • Configure two resource reference files on the same data source using Rational Application Developer.
    1. Edit the ejb-jar.xml file
      1. Create the deployment descriptor if it doesn't already exist:
        1. Go to the context menu of the Enterprise Java beans (EJB) project and select Java EE > Generate Deployment Descriptor Stub.
      2. Edit the deployment descriptor:
        1. Go to the project's META-INF directory and select the ejb-jar.xml file.
      3. Add the enterprise bean's element to the deployment descriptor if it doesn't already exist:
        1. Select EJB Project node. Click Add.
        2. In the dialog, select Enterprise Beans. Click OK.
      4. Add the session bean to the deployment descriptor if it's not already there:
        1. Select Enterprise Beans. Click Add.
        2. Select Session Bean. Click OK.
        3. Enter the name of the session bean in the dialog. Click OK.
        4. In the right-side pane, enter the EJB Class, the business local, and the business remote interfaces.
      5. Add the resource reference elements to the session bean:
        1. Select the session bean and click Add.
        2. Select Resource Reference. Click OK.
        3. In the Add Resource Reference dialog, enter the name, type, authentication and sharing scope fields. Click OK.
        4. Repeat for the second resource reference.
      6. Save the editor
    2. Edit the ibm-ejb-jar-bnd.xml file
      1. Create the WebSphere® EJB bindings descriptor if it doesn't already exist
        1. Go to the context menu of the Enterprise JavaBeans (EJB) project and select Java EE > Generate WebSphere Bindings Deployment Descriptor.
      2. Edit the bindings descriptor:
        1. In the project's META-INF directory, select the ibm-ejb-jar-bnd.xml file.
      3. Add a binding element for the session bean to the bindings descriptor:
        1. Select EJB Jar Bindings node and click Add.
        2. In the dialog, select Session and click OK.
        3. Enter the name of the session bean.
      4. Add the bindings for the resource references to the session bean:
        1. Select the session bean and click Add.
        2. In the dialog, select Resource Reference and click OK.
        3. Select the resource reference.
        4. Enter the name and binding name for the reference
        5. Repeat for the second resource reference.
      5. Save the editor
    3. Edit the ibm-ejb-jar-ext.xml file.
      1. Create the WebSphere EJB extensions descriptor if it doesn't already exist:
        1. Go to the context menu of the Enterprise JavaBeans (EJB) project and select Java EE > Generate WebSphere Extensions Deployment Descriptor.
      2. Edit the extensions descriptor:
        1. In the project's META-INF directory, select the ibm-ejb-jar-ext.xml file.
      3. Add an extensions element for the session bean to the extensions descriptor:
        1. Select the EJB Jar Extensions node and click Add.
        2. In the dialog, select Session and click OK.
        3. Enter the name of the session bean.
      4. Define the isolation level for the resource references:
        1. Select the session bean and click Add.
        2. In the dialog, select Resource Reference and click OK.
        3. Select the resource reference.
        4. Enter the name and the isolation level.
        5. Repeat for the second resource reference.
      5. Save the editor
    4. Edit the persistence.xml file.
      1. Add JPA support to the EJB project, which will create the persistence.xml file:
        1. From the project's context menu, select Properties.
        2. Select the Project Facets node.
        3. Check the box beside Java Persistence.
        4. Click OK.
      2. Edit the persistence.xml file:
        1. In the project's META-INF directory, select the persistence.xml file
      3. The created persistence.xml file already contains a persistence unit definition. Edit this persistence.xml file
        1. Select the Persistence Unit node. Set the name, JTA data source and exclude the unlisted classes fields.
      4. Create a new persistence unit definition in the file:
        1. Select the Persistence node and click Add .
        2. In the dialog, select Persistence Unit and click OK.
        3. Select the Persistence Unit node. Set the name, JTA data source and exclude the unlisted classes fields.
      5. Add classes to the persistence unit:
        1. Select the persistence unit node and click Add .
        2. In the dialog, select Class and click OK.
        3. Enter the name of the class.
        4. Repeat for each class.
      6. Add properties to the persistence unit
        1. If there is not already a Properties element in the file, select the persistence unit node and click Add.
        2. In the dialog, select Properties and click OK.
        3. Select the Properties node and click Add.
        4. In the dialog, select Property and click OK.
        5. Enter the name and value for the property
        6. Repeat the previous three steps to add the additional properties.
      7. Save the editor
  • Configure two resource reference files on the same data source by editing the XML files.
    1. Edit the ejb-jar.xml file:
      <?xml version="1.0" encoding="UTF-8"?>
      <ejb-jar id="ejb-jar_ID" metadata-complete="false" version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" 
      xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" 
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd">
          <enterprise-beans>
              <session>
                  <ejb-name>NewOrderSessionFacadeBean</ejb-name>
                  <business-local>newordersession.ejb3.NewOrderSessionFacade</business-local>
                  <business-remote>newordersession.ejb3.NewOrderSessionFacadeRemote</business-remote>
                  <ejb-class>newordersession.ejb3.NewOrderSessionFacadeBean</ejb-class>
                  <session-type>Stateless</session-type>
      			<resource-ref>
      				<description></description>
      				<res-ref-name>jdbc/ERWWDataSourceV5</res-ref-name>
      				<res-type>javax.sql.DataSource</res-type>
      				<res-auth>Container</res-auth>
      				<res-sharing-scope>Shareable</res-sharing-scope>
      			</resource-ref>
      			<resource-ref>
      				<description></description>
      				<res-ref-name>jdbc/ERWWDataSourceV5_HP</res-ref-name>
      				<res-type>javax.sql.DataSource</res-type>
      				<res-auth>Container</res-auth>
      				<res-sharing-scope>Shareable</res-sharing-scope>
      			</resource-ref>
              </session>
           </enterprise-beans>
      </ejb-jar>
    2. Edit the ibm-ejb-jar-bnd.xml file:
      <?xml version="1.0" encoding="UTF-8"?>
      <ejb-jar-bnd xmlns="http://websphere.ibm.com/xml/ns/javaee"
      	xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
      	xsi:schemaLocation="http://websphere.ibm.com/xml/ns/javaee http://websphere.ibm.com/xml/ns/javaee/ibm-ejb-jar-bnd_1_0.xsd"
      	version="1.0">
      	<session name="NewOrderSessionFacadeBean" simple-binding-name="ejb/session/NewOrderSessionFacadeBean">
      		<resource-ref name="jdbc/ERWWDataSourceV5" binding-name="jdbc/ERWWDataSourceV5"></resource-ref>
      		<resource-ref name="jdbc/ERWWDataSourceV5_HP" binding-name="jdbc/ERWWDataSourceV5"></resource-ref>
      	</session>
      </ejb-jar-bnd>
      
    3. Edit the ibm-ejb-jar-ext.xml file:
      <?xml version="1.0" encoding="UTF-8"?>
      <ejb-jar-ext xmlns="http://websphere.ibm.com/xml/ns/javaee"
      	xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
      	xsi:schemaLocation="http://websphere.ibm.com/xml/ns/javaee http://websphere.ibm.com/xml/ns/javaee/ibm-ejb-jar-ext_1_0.xsd"
      	version="1.0" metadata-complete="true">
      	<session name="NewOrderSessionFacadeBean">
      		<resource-ref name="jdbc/ERWWDataSourceV5"
      			isolation-level="TRANSACTION_READ_COMMITTED" />
      		<resource-ref name="jdbc/ERWWDataSourceV5_HP"
      			isolation-level="TRANSACTION_READ_COMMITTED" />
      	</session>
      </ejb-jar-ext>
    4. Edit the persistence.xml file:
      <?xml version="1.0" encoding="UTF-8"?>
      <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
       version="1.0" xsi:schemaLocation=
      "http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
          <persistence-unit name="NewOrderSessionEJB3">
              <jta-data-source>java:comp/env/jdbc/ERWWDataSourceV5</jta-data-source>
              <class>warehouseejb3.WarehouseJPA</class>
              <class>districtejb3.DistrictJPA</class>
              <class>customerejb3.CustomerJPA</class>
              <class>stockejb3.StockJPA</class>
              <class>orderejb3.OrderJPA</class>
              <class>orderlineejb3.OrderlineJPA</class>
              <class>neworderejb3.NewOrderJPA</class>
              <exclude-unlisted-classes>true</exclude-unlisted-classes>
              <properties>
      		<property name="openjpa.LockManager" value="pessimistic"/>
      		<property name="openjpa.ReadLockLevel" value="read"/>
      		<property name="openjpa.WriteLockLevel" value="write"/>
      		<property name="openjpa.LockTimeout" value="30000"/>
          	<property name="openjpa.FetchBatchSize" value="1" />
          	<property name="openjpa.jdbc.TransactionIsolation" value="read-committed" /> 
          	<property name="openjpa.Log" value="none"/>
          	</properties>	
          </persistence-unit>  
          <persistence-unit name="ItemEJB3">
              <jta-data-source>java:comp/env/jdbc/ERWWDataSourceV5_HP</jta-data-source>
              <class>itemejb3.ItemJPA</class>
              <exclude-unlisted-classes>true</exclude-unlisted-classes>
              <properties>
      		<property name="openjpa.LockManager" value="pessimistic"/>
      		<property name="openjpa.ReadLockLevel" value="read"/>
      		<property name="openjpa.WriteLockLevel" value="write"/>
      		<property name="openjpa.LockTimeout" value="30000"/>
          	<property name="openjpa.FetchBatchSize" value="1" />
          	<property name="openjpa.jdbc.TransactionIsolation" value="read-committed" /> 
          	<property name="openjpa.Log" value="none"/>
          	</properties>	
          </persistence-unit>  
      </persistence>