Java Persistence
API (JPA) applications specify the underlying data source that is
used by the persistence provider to access the database.
About this task
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 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 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
- Select the name of the data source that you want to configure.
- Select WebSphere Application Server data source properties from
the Additional Properties heading.
- Select Non-transactional data source.
- Click OK.
Avoid trouble: 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.
You can find the level that is used for your database by reading the
topic, Requirements for setting isolation level.
If the default
for your database is not
READ_COMMITTED, you
can change the default by adding an additional data source custom
property webSphereDefaultIsolationLevel.
Table 1. Isolation
level values. This table shows valid isolation level
values.Value |
Isolation Level |
1 |
READ_UNCOMMITTED |
2 |
READ_COMMITTED (JPA default) |
4 |
REPEATABLE_READ (WebSphere Application Server default) |
8 |
SERIALIZABLE |
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
WebSphere Application Server, you
can do this by adding the following properties to
persistence.xml file:
<property name="openjpa.Optimistic" value="false"/>
<property name="openjpa.LockManager" value=pessimistic"/>
gotcha
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 WebSphere Application
Server 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. You 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>
- You 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
You can use alternative standard JPA properties
that are equivalent to the OpenJPA properties, such as:
Table 2. 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.