Associating persistence providers and data sources
Java Persistence API (JPA) applications specify the underlying data source used by the persistence provider to access the database.
The application server provides three methods for defining the data sources in the persistence.xml file.
Tasks
- Explicitly specify the JNDI name in the persistence.xml file, and the application directly references the data source. Switching to another data source requires an update to the persistence.xml file.
JPA has two transactional patterns for accessing a 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 EJB session facade. This supports 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.
- The non-JTA resource pattern is used when dealing 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.
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 configured for the application server do not function as a <non-jta-data-source>, because all data sources configured by the application server are automatically enlisted with the current transactional context. To prevent this automatic enlistment, add an additional data source custom property nonTransactionalDataSource=true:
- Select Resources > JDBC > Data sources
- Select the name of the data source to configure.
- Select WAS data source properties from the Additional Properties heading.
- Select Non-transactional data source.
- Click OK.
The JPA specification assumes that connections are obtained with an isolation level that does not hold long-term locks in the database, such as READ_COMMITTED. This might not match the WebSphere Application Server default isolation level, which is REPEATABLE_READ for most databases. We can find the level used for the database by reading the topic, Requirements for setting isolation level.
If the default for the database is not READ_COMMITTED, we can change the default by adding an additional data source custom property webSphereDefaultIsolationLevel.
If the isolation level is set to a value that holds long-term read locks, configure the JPA provider to use Pessimistic Locking instead of the default Optimistic Locking.
Value Isolation Level 1 READ_UNCOMMITTED 2 READ_COMMITTED (JPA default) 4 REPEATABLE_READ (WAS default) 8 SERIALIZABLE For EclipseLink, use the following property in your persistence.xml file:
<property name="eclipselink.pessimistic-lock" value="Lock"/>For OpenJPA, use the following properties in your persistence.xml file:
<property name="openjpa.Optimistic" value="false"/> <property name="openjpa.LockManager" value=pessimistic"/>The JPA specification mandates that the data sources defined in <jta-data-source> and <non-jta-data-source> elements of a persistence unit register in the JNDI name space.
For example, the persistence.xml file should contain an entry like the following:<jta-data-source>jdbc/DataSourceJNDI</jta-data-source>- The JPA for WAS solution extends the JNDI data-source implementation to allow you to reference data sources in the component name space. In the EJB or web module deployment descriptor file, this is the <resource-ref> element. We can prefix the data source with java:comp/env/ so the application indirectly references the data source using the local JNDI name. In this association, the application does not require updates, we change <resource-ref> to use another data source. See the following example:
<jta-data-source>java:comp/env/jdbc/DataSourceJNDI</jta-data-source>
What to do next
For information about configuring data sources, see the topic on creating and configuring a data source.
Related:
Requirements for setting data access isolation levels Configure a JDBC provider and data source Developing JPA 2.x applications for a Java EE environment Developing JPA 2.x applications for a Java SE environment Troubleshoot JPA applications