You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: documentation/src/main/asciidoc/introduction/Tuning.adoc
+19-2Lines changed: 19 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1009,8 +1009,14 @@ Indeed, the usual practice is to avoid having transactions that span user intera
1009
1009
That said, there _is_ also a place for pessimistic locks, which can sometimes reduce the probability of transaction rollbacks.
1010
1010
1011
1011
Therefore, the `find()`, `lock()`, and `refresh()` methods of the session accept an optional link:{doc-javadoc-url}/org/hibernate/LockMode.html[`LockMode`].
1012
-
We can also specify a `LockMode` for a query.
1013
-
The lock mode can be used to request a pessimistic lock, or to customize the behavior of optimistic locking:
1012
+
Here's the simplest way to execute a `select ... for update` in Hibernate:
1013
+
1014
+
[source,java]
1015
+
Book book = session.find(Book.class, isbn, LockMode.PESSIMISTIC_WRITE);
1016
+
1017
+
We can also link:{doc-javadoc-url}/org/hibernate/query/SelectionQuery.html#setLockMode(jakarta.persistence.LockModeType)[specify] a `LockMode` for a query.
1018
+
1019
+
A lock mode can be used to request a pessimistic lock, or to customize the behavior of optimistic locking:
1014
1020
1015
1021
.Optimistic and pessimistic lock modes
1016
1022
[%breakable,cols="26,~"]
@@ -1048,6 +1054,17 @@ However, JPA's `LockModeType.READ` is a synonym for `OPTIMISTIC` -- it's not the
1048
1054
Similarly, `LockModeType.WRITE` is a synonym for `OPTIMISTIC_FORCE_INCREMENT` and is not the same as `LockMode.WRITE`.
1049
1055
====
1050
1056
1057
+
A pessimistic lock request may be combined with an explicit `Timeout`.
The interface link:{doc-javadoc-url}/org/hibernate/Timeouts.html[`Timeouts`] defines some special instances of `Timeout` which may be used to request use of link:{doc-javadoc-url}/org/hibernate/Timeouts.html#NO_WAIT[`for update nowait`] or link:{doc-javadoc-url}/org/hibernate/Timeouts.html#SKIP_LOCKED[`for update skip locked`] on databases which support these options.
0 commit comments