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/Entities.adoc
+1-3Lines changed: 1 addition & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -458,9 +458,7 @@ It's easy to specify that the initial version should be assigned the number `1`
458
458
int version = 1; // the initial version number
459
459
----
460
460
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.
Copy file name to clipboardExpand all lines: documentation/src/main/asciidoc/introduction/Interacting.adoc
+8-2Lines changed: 8 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -301,9 +301,15 @@ The persistence context is fragile.
301
301
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.
302
302
====
303
303
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
+
304
310
[CAUTION]
305
311
====
306
-
Some of these operations require slightly more care than others.
312
+
Some of these operations listed above require slightly more care than others.
307
313
When you call `detach()`, `clear()`, `flush()`, or `refresh()`, you've already strayed from the narrow path.
308
314
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.
309
315
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
1315
1321
| `byMultipleIds()` | Lets us load a _batch_ of ids at the same time
1316
1322
|===
1317
1323
1318
-
[WARNING]
1324
+
[NOTE]
1319
1325
====
1320
1326
Since the introduction of `FindOption` in JPA 3.2, `byId()` is now much less useful.
Copy file name to clipboardExpand all lines: documentation/src/main/asciidoc/introduction/Tuning.adoc
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -924,7 +924,7 @@ When many transactions try to read and update the same data, the program might b
924
924
925
925
There's two basic approaches to data concurrency in Hibernate:
926
926
927
-
- optimistic locking using `@Version` columns, and
927
+
- optimistic locking using <<version-attributes,`@Version` columns>>, and
928
928
- database-level pessimistic locking using the SQL `for update` syntax (or equivalent).
929
929
930
930
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
938
938
939
939
That said, there _is_ also a place for pessimistic locks, which can sometimes reduce the probability of transaction rollbacks.
940
940
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.
943
943
The lock mode can be used to request a pessimistic lock, or to customize the behavior of optimistic locking:
0 commit comments