Skip to content

Commit eb47e6b

Browse files
committed
improve coverage of optimistic locking in the Short Guide
1 parent 1a8cbad commit eb47e6b

File tree

3 files changed

+12
-8
lines changed

3 files changed

+12
-8
lines changed

documentation/src/main/asciidoc/introduction/Entities.adoc

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -458,9 +458,7 @@ It's easy to specify that the initial version should be assigned the number `1`
458458
int version = 1; // the initial version number
459459
----
460460

461-
Optimistic locks are verified by checking versions.
462-
A version check is included in the `where` clause of every SQL `update` or `delete` statement for a versioned entity.
463-
If a version check fails--that is, if no rows are updated--Hibernate throws an `OptimisticLockException` indicating that the current session is working with stale data--that is, that the entity was updated in some other unit of work.
461+
Almost every entity which is frequently updated should have a version attribute.
464462

465463
[TIP]
466464
// .Optimistic locking in Hibernate

documentation/src/main/asciidoc/introduction/Interacting.adoc

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,9 +301,15 @@ The persistence context is fragile.
301301
If you receive an exception from Hibernate, you should immediately close and discard the current session. Open a new session if you need to, but throw the bad one away first.
302302
====
303303

304+
One very important kind of exception which can happen when data is shared between concurrent units of work is an _optimistic lock failure_.
305+
Optimistic locks are verified by checking <<version-attributes,versions>>.
306+
A version check is included in the `where` clause of every SQL `update` or `delete` statement for a versioned entity.
307+
If a version check fails--that is, if no rows are updated--Hibernate infers that the entity was updated in some other unit of work and throws an `OptimisticLockException` to indicate that the current session is working with stale data.
308+
As with other exceptions, this loss of synchronization between the persistence context and the database means that we must discard the current session.
309+
304310
[CAUTION]
305311
====
306-
Some of these operations require slightly more care than others.
312+
Some of these operations listed above require slightly more care than others.
307313
When you call `detach()`, `clear()`, `flush()`, or `refresh()`, you've already strayed from the narrow path.
308314
You didn't stray far--and you probably had a good reason for going there--but you're in territory where Hibernate just has to assume you know what you're doing.
309315
If you start to feel that this terrain is bogging you down, consider using a <<stateless-sessions,stateless session>>.
@@ -1315,7 +1321,7 @@ Therefore, Hibernate has some APIs that streamline certain more complicated look
13151321
| `byMultipleIds()` | Lets us load a _batch_ of ids at the same time
13161322
|===
13171323

1318-
[WARNING]
1324+
[NOTE]
13191325
====
13201326
Since the introduction of `FindOption` in JPA 3.2, `byId()` is now much less useful.
13211327
====

documentation/src/main/asciidoc/introduction/Tuning.adoc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -924,7 +924,7 @@ When many transactions try to read and update the same data, the program might b
924924

925925
There's two basic approaches to data concurrency in Hibernate:
926926

927-
- optimistic locking using `@Version` columns, and
927+
- optimistic locking using <<version-attributes,`@Version` columns>>, and
928928
- database-level pessimistic locking using the SQL `for update` syntax (or equivalent).
929929

930930
In the Hibernate community it's _much_ more common to use optimistic locking, and Hibernate makes that incredibly easy.
@@ -938,8 +938,8 @@ Indeed, the usual practice is to avoid having transactions that span user intera
938938

939939
That said, there _is_ also a place for pessimistic locks, which can sometimes reduce the probability of transaction rollbacks.
940940

941-
Therefore, the `find()`, `lock()`, and `refresh()` methods of the reactive session accept an optional `LockMode`.
942-
We can also specify a link:{doc-javadoc-url}/org/hibernate/LockMode.html[`LockMode`] for a query.
941+
Therefore, the `find()`, `lock()`, and `refresh()` methods of the session accept an optional link:{doc-javadoc-url}/org/hibernate/LockMode.html[`LockMode`].
942+
We can also specify a `LockMode` for a query.
943943
The lock mode can be used to request a pessimistic lock, or to customize the behavior of optimistic locking:
944944

945945
.Optimistic and pessimistic lock modes

0 commit comments

Comments
 (0)