Java Persistence
API (JPA) access intent specifies the isolation level and lock level
used when reading data from a data source. Access intent controls
the Java Database Connectivity
(JDBC) isolation level and whether read, update, or exclusive locks
are acquired when retrieving data.
About this task
For a JPA persistence provider on the application
server,
the application can specify isolation and ReadLockMode based on a
TaskName. The TaskName provides a better control over applying these
characteristics. The application defines a set of entity types and
corresponding access intent for each TaskName defined in a persistence
unit.
Restriction: - Access intent is available for
application in the Java EE server
environment
- Access intent is applicable to non-query entity
manager interface
methods. Query uses query hint interface to set its isolation and
read lock values.
- Access intent is only available for DB2® databases.
- Access intent
is in effect only when pessimistic lock manager
is used. Add the following to the persistence unit property list. <property
name="openjpa.LockManager" value="pessimistic"/>
Table 1. Access intent Properties and Descriptions. The
following table compares the Enterprise JavaBeans (EJB) 2.x entity bean access
intent with the JPA access intent properties:
WebSphere® EJB 2.x entity bean access intent |
JPA access intent |
Description |
optimistic |
isolation: Read Committed |
Data is read but no lock is held.
Version ID is used on update to ensure data integrity. Other transactions
can read and update data. |
lockManager:
Optimistic |
query Hint:
ReadLockMode: READ |
pessimistic read |
isolation:
Repeatable Read |
Data is read with shared locks.
Other transactions attempting to update data are blocked. |
lockManager: Optimistic |
query Hint: ReadLockMode: READ |
pessimistic update |
isolation: Repeatable Read |
Data is retrieved with update or
exclusive lock. Other writes are blocked until commit. This access
intent can be used to serialize update access to data when there are
multiple writers. |
lockManager:
Pessimistic |
query Hint:
ReadLockMode: WRITE |
pessimistic exclusive |
isolation:
Serializable |
Data
is retrieved with update or
exclusive lock. Other writes are blocked until commit. This access
intent can be used to serialize update access to data when there are
multiple writers. |
lockManager:
Pessimistic |
query Hint:
ReadLockMode:WRITE |
A
TaskName is set on a transaction context by one of
the following:
- TaskName is automatically set in the EJB container
when a transaction
begins using WebSphere local
transaction (EJB unspecified transaction), JTA global transaction
in a Container-Managed Transaction (CMT) or user-initiated global
transaction in a Bean-Managed Transaction (BMT).
- TaskName
is manually set in an application using the TaskNameAccessor
API provided for JPA.
Using task names supports the
specification of access
intent on a request scope rather than specifying it in the persistence.xml file,
which has an application scope across all entities. Often a query
is contained in a method or component which is used in many different
transaction contexts. Some of these contexts might require repeatable-read
and update lock intent but other contexts do not.
Isolation
level and read locks can be specified on:
- An application scope
in the persistence.xml file.
These isolation levels and read lock types are properties specified
in the persistence.xml file. They apply to all
entities that are defined in the persistence unit.
- A transaction
scope in the task name. Transaction scoped hints
override application scope values.
- Query instance with a query
hint. Query hint can be used to override
isolation and ReadLockMode for a particular query instance. A query
hint overrides isolation level and read locks specified at the application
or transaction scope.
Procedure
- Setting a TaskName using TaskNameAccessor API This
task explains how to use the TaskNameAccessor API to set JPA TaskName
at run time.
- Specifying TaskName in a JPA persistence unit This
task explains how to specify a TaskName in JPA persistence unit .
What to do next
For more information about Access intent,
see the topic,
Access intent service.