Skip to content

Commit 945ec12

Browse files
committed
correctly implement the documented semantics of beginTransaction()
and clarify its semantics in the jdoc
1 parent b1eb194 commit 945ec12

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,17 @@ public interface SharedSessionContract extends QueryProducer, AutoCloseable, Ser
6868
* Begin a unit of work and return the associated {@link Transaction} object.
6969
* If a new underlying transaction is required, begin the transaction. Otherwise,
7070
* continue the new work in the context of the existing underlying transaction.
71+
* <p>
72+
* The JPA-standard way to begin a new transaction is by calling
73+
* {@link #getTransaction getTransaction().begin()}. When
74+
* {@linkplain org.hibernate.jpa.spi.JpaCompliance#isJpaTransactionComplianceEnabled
75+
* strict JPA transaction compliance} is enabled via, for example, setting
76+
* {@value org.hibernate.cfg.JpaComplianceSettings#JPA_TRANSACTION_COMPLIANCE},
77+
* or when resource-local transactions are used, the call to {@code begin()}
78+
* fails if the transaction is already {@linkplain Transaction#isActive active}.
79+
* On the other hand, this method does not fail when a transaction is already
80+
* active, and simply returns the {@link Transaction} object representing the
81+
* active transaction.
7182
*
7283
* @return an instance of {@link Transaction} representing the new transaction
7384
*

hibernate-core/src/main/java/org/hibernate/internal/AbstractSharedSessionContract.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -610,9 +610,13 @@ public CacheTransactionSynchronization getCacheTransactionSynchronization() {
610610
@Override
611611
public Transaction beginTransaction() {
612612
checkOpen();
613-
final Transaction result = getTransaction();
614-
result.begin();
615-
return result;
613+
final Transaction transaction = getTransaction();
614+
// only need to begin a transaction if it was not
615+
// already active (this is the documented semantics)
616+
if ( !transaction.isActive() ) {
617+
transaction.begin();
618+
}
619+
return transaction;
616620
}
617621

618622
protected void checkTransactionSynchStatus() {

0 commit comments

Comments
 (0)