9. Locking
In a relational database, locking refers to actions taken to prevent data from changing between the time it is read and the time is used.
Your locking strategy can be either optimistic or pessimistic.
- Optimistic
Optimistic locking assumes that multiple transactions can complete without affecting each other, and that therefore transactions can proceed without locking the data resources that they affect. Before committing, each transaction verifies that no other transaction has modified its data. If the check reveals conflicting modifications, the committing transaction rolls back.
Pessimistic
- Pessimistic locking assumes that concurrent transactions will conflict with each other, and requires resources to be locked after they are read and only unlocked after the application has finished using the data.
Hibernate provides mechanisms for implementing both types of locking in your applications.
9.1. Optimistic
When your application uses long transactions or conversations that span several database transactions, you can store versioning data, so that if the same entity is updated by two conversations, the last to commit changes is informed of the conflict, and does not override the other conversation’s work. This approach guarantees some isolation, but scales well and works particularly well in read-often-write-sometimes situations.
Hibernate provides two different mechanisms for storing versioning information, a dedicated version number or a timestamp.
A version or timestamp property can never be null for a detached instance. Hibernate detects any instance with a null version or timestamp as transient, regardless of other unsaved-value strategies that you specify. Declaring a nullable version or timestamp property is an easy way to avoid problems with transitive reattachment in Hibernate, especially useful if you use assigned identifiers or composite keys. | |
---|---|
9.2. Dedicated version number
The version number mechanism for optimistic locking is provided through a @Version
annotation.
Example 203. @Version annotation
@Version
private long version;
Here, the version property is mapped to the version
column, and the entity manager uses it to detect conflicting updates, and prevent the loss of updates that would otherwise be overwritten by a last-commit-wins strategy.
The version column can be any kind of type, as long as you define and implement the appropriate UserVersionType
.
Your application is forbidden from altering the version number set by Hibernate. To artificially increase the version number, see the documentation for properties LockModeType.OPTIMISTIC_FORCE_INCREMENT
or LockModeType.PESSIMISTIC_FORCE_INCREMENT
check in the Hibernate Entity Manager reference documentation.
If the version number is generated by the database, such as a trigger, use the annotation @org.hibernate.annotations.Generated(GenerationTime.ALWAYS) on the version attribute. |
|
---|---|
9.3. Timestamp
Timestamps are a less reliable way of optimistic locking than version numbers, but can be used by applications for other purposes as well. Timestamping is automatically used if you the @Version
annotation on a Date
or Calendar
property type.
Example 204. Using timestamps for optimistic locking
@Version
private Date version;
Hibernate can retrieve the timestamp value from the database or the JVM, by reading the value you specify for the @org.hibernate.annotations.Source
annotation. The value can be either org.hibernate.annotations.SourceType.DB
or org.hibernate.annotations.SourceType.VM
. The default behavior is to use the database, and is also used if you don’t specify the annotation at all.
The timestamp can also be generated by the database instead of Hibernate, if you use the @org.hibernate.annotations.Generated(GenerationTime.ALWAYS)
annotation.
9.4. Pessimistic
Typically, you only need to specify an isolation level for the JDBC connections and let the database handle locking issues. If you do need to obtain exclusive pessimistic locks or re-obtain locks at the start of a new transaction, Hibernate gives you the tools you need.
Hibernate always uses the locking mechanism of the database, and never lock objects in memory. | |
---|---|
9.5. The LockMode
class
The LockMode
class defines the different lock levels that Hibernate can acquire.
LockMode.WRITE | acquired automatically when Hibernate updates or inserts a row. |
---|---|
LockMode.UPGRADE | acquired upon explicit user request using SELECT … FOR UPDATE on databases which support that syntax. |
LockMode.UPGRADE_NOWAIT | acquired upon explicit user request using a SELECT … FOR UPDATE NOWAIT in Oracle. |
LockMode.UPGRADE_SKIPLOCKED | acquired upon explicit user request using a SELECT … FOR UPDATE SKIP LOCKED in Oracle, or SELECT … with (rowlock, updlock, readpast) in SQL Server . |
LockMode.READ | acquired automatically when Hibernate reads data under Repeatable Read or Serializable isolation level. It can be re-acquired by explicit user request. |
LockMode.NONE | The absence of a lock. All objects switch to this lock mode at the end of a Transaction. Objects associated with the session via a call to update() or saveOrUpdate() also start out in this lock mode. |
The explicit user request mentioned above occurs as a consequence of any of the following actions:
a call to
Session.load()
, specifying aLockMode
.a call to
Session.lock()
.a call to
Query.setLockMode()
.
If you call Session.load()
with option UPGRADE
, UPGRADE_NOWAIT
or UPGRADE_SKIPLOCKED
, and the requested object is not already loaded by the session, the object is loaded using SELECT … FOR UPDATE
.
If you call load()
for an object that is already loaded with a less restrictive lock than the one you request, Hibernate calls lock()
for that object.
Session.lock(
) performs a version number check if the specified lock mode is READ
, UPGRADE
, UPGRADE_NOWAIT
or UPGRADE_SKIPLOCKED
. In the case of UPGRADE
, UPGRADE_NOWAIT
or UPGRADE_SKIPLOCKED
, the SELECT … FOR UPDATE
syntax is used.
If the requested lock mode is not supported by the database, Hibernate uses an appropriate alternate mode instead of throwing an exception. This ensures that applications are portable.
9.6. JPA locking query hints
JPA 2.0 introduced two query hints:
- javax.persistence.lock.timeout
it gives the number of milliseconds a lock acquisition request will wait before throwing an exception
javax.persistence.lock.scope
- defines the scope of the lock acquisition request. The scope can either be
NORMAL
(default value) orEXTENDED
. TheEXTENDED
scope will cause a lock acquisition request to be passed to other owned table structured (e.g.@Inheritance(strategy=InheritanceType.JOINED)
,@ElementCollection
)
Example 205. javax.persistence.lock.timeout
example
entityManager.find(
Person.class, id, LockModeType.PESSIMISTIC_WRITE,
Collections.singletonMap( "javax.persistence.lock.timeout", 200 )
);
SELECT explicitlo0_.id AS id1_0_0_,
explicitlo0_."name" AS name2_0_0_
FROM person explicitlo0_
WHERE explicitlo0_.id = 1
FOR UPDATE wait 2
Not all JDBC database drivers support setting a timeout value for a locking request. If not supported, the Hibernate dialect ignores this query hint. | |
---|---|
The javax.persistence.lock.scope is not yet supported as specified by the JPA standard. |
|
---|---|
9.7. The buildLockRequest
API
Traditionally, Hibernate offered the Session#lock()
method for acquiring an optimistic or a pessimistic lock on a given entity. Because varying the locking options was difficult when using a single LockMode
parameter, Hibernate has added the Session#buildLockRequest()
method API.
The following example shows how to obtain shared database lock without waiting for the lock acquisition request.
Example 206. buildLockRequest
example
Person person = entityManager.find( Person.class, id );
Session session = entityManager.unwrap( Session.class );
session
.buildLockRequest( LockOptions.NONE )
.setLockMode( LockMode.PESSIMISTIC_READ )
.setTimeOut( LockOptions.NO_WAIT )
.lock( person );
SELECT p.id AS id1_0_0_ ,
p.name AS name2_0_0_
FROM Person p
WHERE p.id = 1
SELECT id
FROM Person
WHERE id = 1
FOR SHARE NOWAIT