Skip to content

Commit 5cc6eed

Browse files
committed
correctly implement the documented semantics of beginTransaction()
and clarify its semantics in the jdoc
1 parent 453f0ff commit 5cc6eed

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
@@ -606,9 +606,13 @@ public CacheTransactionSynchronization getCacheTransactionSynchronization() {
606606
@Override
607607
public Transaction beginTransaction() {
608608
checkOpen();
609-
final Transaction result = getTransaction();
610-
result.begin();
611-
return result;
609+
final Transaction transaction = getTransaction();
610+
// only need to begin a transaction if it was not
611+
// already active (this is the documented semantics)
612+
if ( !transaction.isActive() ) {
613+
transaction.begin();
614+
}
615+
return transaction;
612616
}
613617

614618
protected void checkTransactionSynchStatus() {

0 commit comments

Comments
 (0)