WAS v8.5 > Develop applications > Develop EJB applications > Assemble applications that use the Java Persistence APIAssociate 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.
- 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.
Isolation level values. This table shows valid isolation level values.
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. For the JPA provider included with WAS, we can do this by adding the following properties to persistence.xml file:
Value Isolation Level 1 READ_UNCOMMITTED 2 READ_COMMITTED (JPA default) 4 REPEATABLE_READ (WAS default) 8 SERIALIZABLE <property name="openjpa.Optimistic" value="false"/> <property name="openjpa.LockManager" value=pessimistic"/>
The JPA specification mandates the data sources that are 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 us 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, you change <resource-ref> to use another data source. Example:
<jta-data-source>java:comp/env/jdbc/DataSourceJNDI</jta-data-source>
- We can declare openjpa.Connection* properties in the persistence unit as follows:
<property name="openjpa.ConnectionDriverName" value="org.apache.derby.jdbc.EmbeddedDriver" /> <property name="openjpa.ConnectionURL" value="jdbc:derby:target/database/jpa-test-database;create=true"/>
OR
We can use alternative standard JPA properties that are equivalent to the OpenJPA properties, such as:
Standard JPA 2.0 property equivalents. Standard JPA 2.0 properties and the OpenJPA equivalents.
Standard JPA 2.0 OpenJPA Equivalent javax.persistence.jdbc.driver openjpa.ConnectionDriverName javax.persistence.jdbc.url openjpa.ConnectionURL
For information about configuring data sources, see the topic on creating and configuring a data source.
For information about data sources and JPA, see the section on persistence in the Apache OpenJPA User Guide.
Related concepts:
Requirements for setting data access isolation levels
Related
Configure a JDBC provider and data source
Develop JPA 2.x applications for a Java EE environment
Develop JPA 2.x applications for a Java SE environment
Troubleshooting JPA applications
Reference:
Apache OpenJPA User Guide: Persistence
Related information: