Skip to content

Commit 13834c6

Browse files
committed
document the rule for transaction rollback
1 parent adf8e41 commit 13834c6

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,14 @@ entityManager.getTransaction().setRollbackOnly();
233233

234234
A transaction in rollback-only mode will be rolled back when it completes.
235235

236+
[[after-tx-rollback]]
237+
[IMPORTANT]
238+
====
239+
Hibernate is not a software transactional memory.
240+
When a transaction rolls back, Hibernate makes no attempt to roll back the state of objects held in memory to their state at the beginning of the transaction.
241+
After a transaction rollback, the persistence context must be discarded, and the state of its entities must be assumed inconsistent with the state held by the database.
242+
====
243+
236244
[[persistence-operations]]
237245
=== Operations on the persistence context
238246

@@ -294,6 +302,7 @@ Now, if an exception occurs while interacting with the database, there's no good
294302

295303
Therefore, a session is considered to be unusable after any of its methods throws an exception.
296304

305+
[[fragile-persistence-context]]
297306
[IMPORTANT]
298307
// .The persistence context is fragile
299308
====

hibernate-core/src/main/java/org/hibernate/Session.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,9 @@
147147
* cannot be expected to be consistent with the database after the exception occurs.
148148
* <li>At the end of a logical transaction, the session must be explicitly {@linkplain
149149
* #close() destroyed}, so that all JDBC resources may be released.
150+
* <li>If a transaction is rolled back, the state of the persistence context and of its
151+
* associated entities must be assumed inconsistent with the database, and the
152+
* session must be discarded.
150153
* <li>A {@code Session} is never thread-safe. It contains various different sorts of
151154
* fragile mutable state. Each thread or transaction must obtain its own dedicated
152155
* instance from the {@link SessionFactory}.

hibernate-core/src/main/java/org/hibernate/Transaction.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@
2525
* {@code session.getTransaction().begin()}, and ends with a call to {@link #commit()}
2626
* or {@link #rollback()}.
2727
* <p>
28+
* When a transaction rolls back, Hibernate makes no attempt to roll back the state of
29+
* entity instances held in memory to their state at the beginning of the transaction.
30+
* After a transaction rollback, the current {@linkplain Session persistence context}
31+
* must be discarded, and the state of its entities must be assumed inconsistent with
32+
* the state held by the database.
33+
* <p>
2834
* A single session might span multiple transactions since the notion of a session
2935
* (a conversation between the application and the datastore) is of coarser granularity
3036
* than the concept of a database transaction. However, there is at most one uncommitted

0 commit comments

Comments
 (0)