Java Reference
In-Depth Information
pessimistically locking a versioned entity, the persistence provider will perform the ver-
sion checks that occur during optimistic locking, and if the version check fails, an
Op-
timisticLockException
will be thrown. Attempting to lock a non-versioned entity
with
PESSIMISTIC_FORCE_INCREMENT
is not portable and may result in a
Per-
sistenceException
if the persistence provider doesn't support optimistic locks for
non-versioned entities. Locking a versioned entity with
PESSIMISTIC_WRITE
results
in the version attribute being incremented if the transaction was successfully committed.
Pessimistic Locking Timeouts
The length of time in milliseconds the persistence provider should wait to obtain a
lock on the database tables may be specified using the
javax.persistence.lock.timeout
property. If the time it takes to obtain a lock
exceeds the value of this property, a
LockTimeoutException
will be thrown, but the
current transaction will not be marked for rollback. If this property is set to
0
, the per-
sistence provider should throw a
LockTimeoutException
if it cannot immediately
obtain a lock.
Note
Portable applications should not rely on the setting of
javax.persistence.lock.timeout
, as the locking strategy
and underlying database may mean that the timeout value cannot be
used. The value of
javax.persistence.lock.timeout
is a
hint, not a contract.
This property may be set programmatically by passing it to the
EntityManager
methods that allow lock modes to be specified, the
Query.setLockMode
and
TypedQuery.setLockMode
methods, the
@NamedQuery
annotation, and as a prop-
erty to the
Persistence.createEntityManagerFactory
method. It may also
be set as a property in the
persistence.xml
deployment descriptor.
If
javax.persistence.lock.timeout
is set in multiple places, the value will be
determined in the following order:
1.
The argument to one of the
EntityManager
or
Query
methods.
2.
The setting in the
@NamedQuery
annotation.
3.
The
argument
to
the
Persistence.createEntityManagerFactory
method.
4.
The value in the
persistence.xml
deployment descriptor.