Skip to content

Commit 608b8b6

Browse files
committed
fix tests which were asserting stuff about JPA compliance for a non-JPA method
The JPA spec does not have anything to say about our beginTransaction() method
1 parent ecb655f commit 608b8b6

File tree

5 files changed

+45
-12
lines changed

5 files changed

+45
-12
lines changed

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

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,29 @@ 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
71+
*
72+
* @apiNote
73+
* The JPA-standard way to begin a new resource-local transaction is by calling
74+
* {@link #getTransaction getTransaction().begin()}. But it's not always safe to
75+
* execute this idiom.
76+
* <ul>
77+
* <li>JPA doesn't allow an {@link jakarta.persistence.EntityTransaction
78+
* EntityTransaction} to represent a JTA transaction context. Therefore, when
7479
* {@linkplain org.hibernate.jpa.spi.JpaCompliance#isJpaTransactionComplianceEnabled
7580
* strict JPA transaction compliance} is enabled via, for example, setting
7681
* {@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.
82+
* the call to {@code getTransaction()} fails if transactions are managed by JTA.
83+
* <p>
84+
* On the other hand, this method does not fail when JTA transaction management
85+
* is used, not even if strict JPA transaction compliance is enabled.
86+
* <li>Even when resource-local transactions are in use, and even when strict JPA
87+
* transaction compliance is <em>disabled</em>, the call to {@code begin()}
88+
* fails if a transaction is already {@linkplain Transaction#isActive active}.
89+
* <p>
90+
* This method never fails when a transaction is already active. Instead,
91+
* {@code beginTransaction()} simply returns the {@link Transaction} object
92+
* representing the active transaction.
93+
* </ul>
8294
*
8395
* @return an instance of {@link Transaction} representing the new transaction
8496
*
@@ -90,6 +102,20 @@ public interface SharedSessionContract extends QueryProducer, AutoCloseable, Ser
90102
/**
91103
* Get the {@link Transaction} instance associated with this session.
92104
*
105+
* @apiNote
106+
* This method is the JPA-standard way to obtain an instance of
107+
* {@link jakarta.persistence.EntityTransaction EntityTransaction}
108+
* representing a resource-local transaction. But JPA doesn't allow an
109+
* {@code EntityTransaction} to represent a JTA transaction. Therefore, when
110+
* {@linkplain org.hibernate.jpa.spi.JpaCompliance#isJpaTransactionComplianceEnabled
111+
* strict JPA transaction compliance} is enabled via, for example, setting
112+
* {@value org.hibernate.cfg.JpaComplianceSettings#JPA_TRANSACTION_COMPLIANCE},
113+
* this method fails if transactions are managed by JTA.
114+
* <p>
115+
* On the other hand, when JTA transaction management is used, and when
116+
* strict JPA transaction compliance is <em>disabled</em>, this method happily
117+
* returns a {@link Transaction} representing the current JTA transaction context.
118+
*
93119
* @return an instance of {@link Transaction} representing the transaction
94120
* associated with this session
95121
*

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* depending on how Hibernate is configured.
1818
* <p>
1919
* Every resource-local transaction is associated with a {@link Session} and begins with
20-
* an explicit call to {@link Session#beginTransaction()}, or, equivalently, with
20+
* an explicit call to {@link Session#beginTransaction()}, or, almost equivalently, with
2121
* {@code session.getTransaction().begin()}, and ends with a call to {@link #commit()}
2222
* or {@link #rollback()}.
2323
* <p>
@@ -31,6 +31,11 @@
3131
* <p>
3232
* A {@code Transaction} object is not threadsafe.
3333
*
34+
* @apiNote JPA doesn't allow an {@link EntityTransaction} to represent a JTA transaction.
35+
* But when {@linkplain org.hibernate.jpa.spi.JpaCompliance#isJpaTransactionComplianceEnabled
36+
* strict JPA transaction compliance} is disabled, as it is by default, Hibernate allows an
37+
* instance of this interface to represent the current JTA transaction context.
38+
*
3439
* @author Anton van Straaten
3540
* @author Steve Ebersole
3641
*

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,7 @@ public CacheTransactionSynchronization getCacheTransactionSynchronization() {
610610
@Override
611611
public Transaction beginTransaction() {
612612
checkOpen();
613-
final Transaction transaction = getTransaction();
613+
final Transaction transaction = accessTransaction();
614614
// only need to begin a transaction if it was not
615615
// already active (this is the documented semantics)
616616
if ( !transaction.isActive() ) {

hibernate-core/src/test/java/org/hibernate/orm/test/resource/transaction/jta/JpaComplianceAlreadyStartedTransactionTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ public void anIllegalStateExceptionShouldBeThrownWhenBeginTxIsCalledWithAnAlread
4545
Transaction tx = null;
4646
try {
4747
// A call to begin() with an active Tx should cause an IllegalStateException
48-
tx = s.beginTransaction();
48+
tx = s.getTransaction();
49+
tx.begin();
4950
}
5051
catch (Exception e) {
5152
if ( tx != null && tx.isActive() ) {

hibernate-core/src/test/java/org/hibernate/orm/test/resource/transaction/jta/NonJpaComplianceAlreadyStartedTransactionTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ public void setUp() {
5050
public void noIllegalStateExceptionShouldBeThrownWhenBeginTxIsCalledWithAnAlreadyActiveTx() throws Exception {
5151
tm.begin();
5252
try (Session s = openSession()) {
53-
Transaction tx = s.beginTransaction();
53+
Transaction tx = s.getTransaction();
54+
tx.begin();
5455
try {
5556
s.persist( new TestEntity( "ABC" ) );
5657
tx.commit();

0 commit comments

Comments
 (0)