Accessing data by using JPA inside of the bundle

About this task

Accessing data by using JPA inside of the bundle assumes that you are working in a bundle that is treated as a Java™ EE module on the server. For example, an OSGi web application bundle that has a JPA facet enabled and contains entities and the logic to access, consume, and display data by using JPA. Another example is a web project that contains JPA entities and the logic to consume data by using JPA that is published in an OSGi application rather than as part of an EAR deployment.

You need to configure your OSGi bundle and add the data sources to WebSphere® Application Server before you can access the JPA persistence units.

Tip:

Ensure that your JPA persistence file contains references to a Java Transaction API (JTA) and non-JTA data source.

JPA has two transactional patterns for accessing a data source:
jta-data-source
The Java Transaction API (JTA) resource pattern depends on global transactions. The JTA resource pattern is typically used within the scope of an Enterprise JavaBeans (EJB) session facade. This configuration allows the session bean to control transaction and security contexts while JPA handles the persistence mappings. In this case, the application does not use the EntityTransaction interface but relies on the EntityManager enlisted with the global transaction when it is accessed.
non-jta-data-source
The non-JTA resource pattern is used to deal with a single resource in the absence of global transactions. The non-JTA resource pattern is typically used within the scope of a web application or an application client. The application controls the transaction with the data source with the EntityTransaction interface.

In persistence.xml files for an OSGi application, the jta-data-source and non-jta-data-source elements access the data sources through a Java Naming and Directory Interface (JNDI) lookup, a JNDI lookup to the service registry, or through Blueprint.

If the JTA and non-JTA data sources are not configured in the persistence.xml file, the default JTA and non-JTA data sources configured for the server are used. By default the values are null. Some JPA entity features require a non-JTA data source to be specified. For example, automatic entity identity generation.

Procedure

  1. Add non-JTA data sources to persistence.xml:
    1. Open persistence.xml in the editor.
    2. In the Overview section look at the components list and select your entity to display the details of your entity.
    3. In the Non-JTA data source field, type the global JNDI name of a non-JTA data source. For example, osgi:service/javax.sql.DataSource/(osgi.jndi.service.name=jdbc/blogdbnojta).
  2. Add JTA data sources to persistence.xml:
    1. Open persistence.xml in the editor.
    2. In the Overview section look at the components list and select your entity to display the details of your entity.
    3. In the JTA data source field, type the global JNDI name of the data source. For example, osgi:service/javax.sql.DataSource/(osgi.jndi.service.name=jdbc/blogdb).
  3. Add a JDBC provider to the WebSphere Application Server administrative console:
    1. Switch to the Servers view.
    2. Right-click your server instance and select Start.
    3. Right-click your server instance and select Administration > Run Administrative Console to open the administrative console.
    4. Click Resources > JDBC > JDBC providers.
    5. Click New in the JDBC providers page. The Create a data source wizard opens.
    6. Follow the instructions in the wizard to create the JDBC provider.
    7. Save your changes,
  4. Add data source definitions to the WebSphere Application Server administrative console:
    1. In the administrative console, click Resources > JDBC > Data sources to open the Data sources page in the console.
    2. In the Data sources page, click New to create a data source definition with a JNDI name set to the JTA connection definition specified in your persistence.xml. For example, jdbc/blogdb.
    3. In the Data sources page, click New to create another data source definition with a JNDI name set to the non-JTA connection definition specified in your persistence.xml. For example, jdbc/blogdbnojta.
    4. When the data source for the non-JTA connection is created, click the definition for the non-JTA connection in the Data sources page of the admin console. The Configuration page opens.
    5. In the Additional Properties section, click WebSphere Application Server data source properties.
    6. Select Non-transactional data source. Within the application server, the use of the <non-jta-data-source> element requires a special configuration for a non-transactional data source. Data sources that are configured for the application server do not function as a <non-jta-data-source> because all data sources that are configured by the application server are automatically enlisted with the current transactional context. To prevent this automatic enlistment, add a data source custom property nonTransactionalDataSource=true.

Results

The persistence.xml and WebSphere Application Server are configured to access JPA in an OSGi bundle.
Icon that indicates the type of topic Task topic
Timestamp icon Last updated: July 17, 2017 21:58

File name: taccessjpainbundle.html