Network Deployment (Distributed operating systems), v8.0 > Develop and deploying applications > Develop EJB applications > Assembling applications that use the Java Persistence API
Associate persistence providers and data sources
Java Persistence API (JPA) applications specify the underlying data source that is 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.
Procedure
- Explicitly specify the Java Naming and Directory Interface (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 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 WAS default isolation level, which is REPEATABLE_READ for most databases. We can find the level used for your database by reading the topic, Requirements for setting isolation level.
- 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 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 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.
If the default for your database is not READ_COMMITTED, you 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. For the JPA provider included with WAS, you can do this by adding the following properties to persistence.xml file:
Isolation level values. This table shows valid isolation level values.
Value Isolation Level 1 READ_UNCOMMITTED 2 READ_COMMITTED (JPA default) 4 REPEATABLE_READ (WAS default) 8 SERIALIZABLE <property name="openjpa.Optimistic"="false"/> <property name="openjpa.LockManager"=pessimistic"/>The JPA specification mandates that 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 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 by using the local JNDI name. In this association, the application does not require updates, you change <resource-ref> to use another data source. See the following example:
<jta-data-source>java:comp/env/jdbc/DataSourceJNDI </jta-data-source>The JPA specification does not require implementations to include data sources referenced in local JNDI names, so this method of reference might not be portable to other implementations of JPA.- 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
What to do next
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.
Requirements for setting data access isolation levels
Configure a JDBC provider and data source
Task overview: Storing and retrieving persistent data with the JPA API
Develop JPA 2.x applications for a Java EE environment
Develop JPA 2.x applications for a Java SE environment
Related
Troubleshoot JPA applications
Apache OpenJPA User Guide