diff --git a/hibernate-core/src/main/java/org/hibernate/internal/AbstractSharedSessionContract.java b/hibernate-core/src/main/java/org/hibernate/internal/AbstractSharedSessionContract.java index d1826ceb31..793b0a922e 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/AbstractSharedSessionContract.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/AbstractSharedSessionContract.java @@ -342,9 +342,7 @@ public abstract class AbstractSharedSessionContract implements SharedSessionCont @Override public boolean isTransactionInProgress() { - return !isClosed() - && transactionCoordinator.isJoined() - && transactionCoordinator.getTransactionDriverControl().getStatus() == TransactionStatus.ACTIVE; + return !isClosed() && transactionCoordinator.isTransactionActive(); } @Override diff --git a/hibernate-core/src/main/java/org/hibernate/resource/transaction/backend/jdbc/internal/JdbcResourceLocalTransactionCoordinatorImpl.java b/hibernate-core/src/main/java/org/hibernate/resource/transaction/backend/jdbc/internal/JdbcResourceLocalTransactionCoordinatorImpl.java index 89390c25c7..1454e168ce 100644 --- a/hibernate-core/src/main/java/org/hibernate/resource/transaction/backend/jdbc/internal/JdbcResourceLocalTransactionCoordinatorImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/resource/transaction/backend/jdbc/internal/JdbcResourceLocalTransactionCoordinatorImpl.java @@ -84,8 +84,7 @@ public class JdbcResourceLocalTransactionCoordinatorImpl implements TransactionC @Override public boolean isJoined() { - return physicalTransactionDelegate != null && physicalTransactionDelegate.getStatus() == TransactionStatus.ACTIVE; - + return physicalTransactionDelegate != null && getTransactionDriverControl().isActive( true ); } @Override diff --git a/hibernate-core/src/main/java/org/hibernate/resource/transaction/spi/TransactionCoordinator.java b/hibernate-core/src/main/java/org/hibernate/resource/transaction/spi/TransactionCoordinator.java index eac0a806b8..da4ee56c8b 100644 --- a/hibernate-core/src/main/java/org/hibernate/resource/transaction/spi/TransactionCoordinator.java +++ b/hibernate-core/src/main/java/org/hibernate/resource/transaction/spi/TransactionCoordinator.java @@ -95,6 +95,14 @@ public interface TransactionCoordinator { int getTimeOut(); + default boolean isTransactionActive() { + return isTransactionActive( true ); + } + + default boolean isTransactionActive(boolean isMarkedRollbackConsideredActive) { + return isJoined() && getTransactionDriverControl().isActive( isMarkedRollbackConsideredActive ); + } + /** * Provides the means for "local transactions" (as transaction drivers) to control the * underlying "physical transaction" currently associated with the TransactionCoordinator. diff --git a/hibernate-core/src/test/java/org/hibernate/jpa/test/transaction/FlushAndTransactionTest.java b/hibernate-core/src/test/java/org/hibernate/jpa/test/transaction/FlushAndTransactionTest.java index b3e788b38f..bb96e02e46 100644 --- a/hibernate-core/src/test/java/org/hibernate/jpa/test/transaction/FlushAndTransactionTest.java +++ b/hibernate-core/src/test/java/org/hibernate/jpa/test/transaction/FlushAndTransactionTest.java @@ -11,6 +11,7 @@ import javax.persistence.EntityManager; import javax.persistence.LockModeType; import javax.persistence.OptimisticLockException; import javax.persistence.PersistenceException; +import javax.persistence.Query; import javax.persistence.RollbackException; import javax.persistence.TransactionRequiredException; @@ -293,4 +294,23 @@ public class FlushAndTransactionTest extends BaseEntityManagerFunctionalTestCase Book.class }; } + + @Test + public void testSetRollbackOnlyAndFlush() throws Exception { + Book book = new Book(); + book.name = "The jungle book"; + EntityManager em = getOrCreateEntityManager(); + em.getTransaction().begin(); + em.getTransaction().setRollbackOnly(); + em.persist( book ); + em.flush(); + em.getTransaction().rollback(); + em.getTransaction().begin(); + Query query = em.createQuery( "SELECT b FROM Book b WHERE b.name = :name" ); + query.setParameter( "name", book.name ); + assertEquals( 0, query.getResultList().size() ); + em.getTransaction().commit(); + em.close(); + } + }