diff --git a/ebean-core/src/main/java/io/ebeaninternal/server/transaction/TransactionManager.java b/ebean-core/src/main/java/io/ebeaninternal/server/transaction/TransactionManager.java index c30bfcb225..a05828073d 100644 --- a/ebean-core/src/main/java/io/ebeaninternal/server/transaction/TransactionManager.java +++ b/ebean-core/src/main/java/io/ebeaninternal/server/transaction/TransactionManager.java @@ -28,8 +28,8 @@ import io.ebeanservice.docstore.api.DocStoreTransaction; import io.ebeanservice.docstore.api.DocStoreUpdateProcessor; import io.ebeanservice.docstore.api.DocStoreUpdates; - import jakarta.persistence.PersistenceException; + import javax.sql.DataSource; import java.sql.Connection; import java.sql.SQLException; @@ -176,13 +176,6 @@ public final SpiTransaction active() { return scopeManager.active(); } - /** - * Return the current active transaction as a scoped transaction. - */ - private ScopedTransaction activeScoped() { - return (ScopedTransaction) scopeManager.active(); - } - /** * Return the current transaction from thread local scope. Note that it may be inactive. */ @@ -499,7 +492,7 @@ public final ScopedTransaction externalBeginTransaction(SpiTransaction transacti */ public final ScopedTransaction beginScopedTransaction(TxScope txScope) { txScope = initTxScope(txScope); - ScopedTransaction txnContainer = activeScoped(); + ScopedTransaction txnContainer = (ScopedTransaction) inScope(); boolean setToScope; boolean nestedSavepoint; @@ -594,6 +587,7 @@ private TxScope initTxScope(TxScope txScope) { private boolean isCreateNewTransaction(SpiTransaction current, TxType type) { switch (type) { case REQUIRED: + return current == null || !current.isActive(); case SUPPORTS: return current == null; case REQUIRES_NEW: diff --git a/ebean-test/src/test/java/org/tests/transaction/TestNestedTransaction.java b/ebean-test/src/test/java/org/tests/transaction/TestNestedTransaction.java index b2bf41825c..9dcb20862a 100644 --- a/ebean-test/src/test/java/org/tests/transaction/TestNestedTransaction.java +++ b/ebean-test/src/test/java/org/tests/transaction/TestNestedTransaction.java @@ -1,8 +1,9 @@ package org.tests.transaction; -import io.ebean.xtest.BaseTestCase; import io.ebean.DB; import io.ebean.Transaction; +import io.ebean.TxScope; +import io.ebean.xtest.BaseTestCase; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.slf4j.Logger; @@ -235,4 +236,30 @@ public void testNested_111() { } assertModified(); } + + @Test + public void test_txn_with_not_supported() { + + try (Transaction txn1 = DB.beginTransaction()) { + assertThat(getInScopeTransaction()).isNotNull(); + getInScopeTransaction().putUserObject("foo", "bar"); + assertThat(Transaction.current()).isNotNull(); + + try (Transaction txn2 = DB.beginTransaction(TxScope.notSupported())) { + // pause txn1 + assertThat(Transaction.current()).isNull(); + + try (Transaction txn3 = DB.beginTransaction()) { + // create a new Txn scope + assertThat(Transaction.current()).isNotNull(); + DB.save(new EBasic()); + txn3.commit(); + } + DB.save(new EBasic()); + txn2.commit(); + } + // resume txn1 + assertThat(getInScopeTransaction().getUserObject("foo")).isEqualTo("bar"); + } + } }