correctly implement the documented semantics of beginTransaction()

and clarify its semantics in the jdoc
This commit is contained in:
Gavin King 2024-11-20 12:51:52 +01:00
parent 453f0ff074
commit 5cc6eed50a
2 changed files with 18 additions and 3 deletions

View File

@ -68,6 +68,17 @@ public interface SharedSessionContract extends QueryProducer, AutoCloseable, Ser
* Begin a unit of work and return the associated {@link Transaction} object.
* If a new underlying transaction is required, begin the transaction. Otherwise,
* continue the new work in the context of the existing underlying transaction.
* <p>
* The JPA-standard way to begin a new transaction is by calling
* {@link #getTransaction getTransaction().begin()}. When
* {@linkplain org.hibernate.jpa.spi.JpaCompliance#isJpaTransactionComplianceEnabled
* strict JPA transaction compliance} is enabled via, for example, setting
* {@value org.hibernate.cfg.JpaComplianceSettings#JPA_TRANSACTION_COMPLIANCE},
* or when resource-local transactions are used, the call to {@code begin()}
* fails if the transaction is already {@linkplain Transaction#isActive active}.
* On the other hand, this method does not fail when a transaction is already
* active, and simply returns the {@link Transaction} object representing the
* active transaction.
*
* @return an instance of {@link Transaction} representing the new transaction
*

View File

@ -606,9 +606,13 @@ public abstract class AbstractSharedSessionContract implements SharedSessionCont
@Override
public Transaction beginTransaction() {
checkOpen();
final Transaction result = getTransaction();
result.begin();
return result;
final Transaction transaction = getTransaction();
// only need to begin a transaction if it was not
// already active (this is the documented semantics)
if ( !transaction.isActive() ) {
transaction.begin();
}
return transaction;
}
protected void checkTransactionSynchStatus() {