diff --git a/hibernate-core/src/test/java/org/hibernate/jpa/test/transaction/JtaReusingEntityTransactionTest.java b/hibernate-core/src/test/java/org/hibernate/jpa/test/transaction/JtaReusingEntityTransactionTest.java deleted file mode 100644 index e6f2b7e9d1..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/jpa/test/transaction/JtaReusingEntityTransactionTest.java +++ /dev/null @@ -1,85 +0,0 @@ -/* - * Hibernate, Relational Persistence for Idiomatic Java - * - * License: GNU Lesser General Public License (LGPL), version 2.1 or later. - * See the lgpl.txt file in the root directory or . - */ -package org.hibernate.jpa.test.transaction; - -import java.util.List; -import javax.persistence.Entity; -import javax.persistence.EntityManager; -import javax.persistence.EntityTransaction; -import javax.persistence.GeneratedValue; -import javax.persistence.Id; - -import org.hibernate.orm.test.jpa.transaction.SynchronizationTypeTest; - -import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; -import org.hibernate.testing.orm.junit.Jpa; -import org.hibernate.testing.orm.junit.Setting; -import org.junit.jupiter.api.Test; - -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.core.Is.is; - -/** - * @author Andrea Boriero - */ -@Jpa( - annotatedClasses = { JtaReusingEntityTransactionTest.TestEntity.class }, - integrationSettings = { - @Setting(name = org.hibernate.jpa.AvailableSettings.TRANSACTION_TYPE, value = "JTA"), - @Setting(name = org.hibernate.cfg.AvailableSettings.CONNECTION_PROVIDER, value = "org.hibernate.testing.jta.JtaAwareConnectionProviderImpl"), - - }, - nonStringValueSettingProviders = { SynchronizationTypeTest.JtaPlatformNonStringValueSettingProvider.class } -) -public class JtaReusingEntityTransactionTest { - - @Test - public void entityTransactionShouldBeReusableTest(EntityManagerFactoryScope scope) { - EntityManager em = createEntityManager( scope ); - EntityTransaction transaction = null; - try { - transaction = em.getTransaction(); - em.persist( new TestEntity() ); - transaction.begin(); - transaction.commit(); - transaction.begin(); - em.persist( new TestEntity() ); - transaction.commit(); - } - finally { - if ( transaction != null && transaction.isActive() ) { - transaction.rollback(); - } - em.close(); - } - em = createEntityManager( scope ); - try { - transaction = em.getTransaction(); - transaction.begin(); - List results = em.createQuery( "from TestEntity" ).getResultList(); - assertThat( results.size(), is( 2 ) ); - transaction.commit(); - } - finally { - if ( transaction != null && transaction.isActive() ) { - transaction.rollback(); - } - em.close(); - } - } - - private EntityManager createEntityManager(EntityManagerFactoryScope scope) { - return scope.getEntityManagerFactory().createEntityManager(); - } - - @Entity(name = "TestEntity") - public static class TestEntity { - @Id - @GeneratedValue - private Long id; - } -} diff --git a/hibernate-core/src/test/java/org/hibernate/jpa/test/transaction/TransactionRollbackTest.java b/hibernate-core/src/test/java/org/hibernate/jpa/test/transaction/TransactionRollbackTest.java deleted file mode 100644 index 1ccc46528f..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/jpa/test/transaction/TransactionRollbackTest.java +++ /dev/null @@ -1,158 +0,0 @@ -/* - * Hibernate, Relational Persistence for Idiomatic Java - * - * License: GNU Lesser General Public License (LGPL), version 2.1 or later. - * See the lgpl.txt file in the root directory or . - */ -package org.hibernate.jpa.test.transaction; - -import javax.persistence.Entity; -import javax.persistence.EntityManager; -import javax.persistence.Id; -import javax.persistence.Version; - -import org.hibernate.Session; -import org.hibernate.engine.transaction.spi.TransactionObserver; -import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; -import org.hibernate.resource.jdbc.spi.JdbcSessionOwner; - -import org.hibernate.testing.TestForIssue; -import org.junit.Test; - -import static org.hamcrest.core.Is.is; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.fail; - -/** - * @author Andrea Boriero - */ -public class TransactionRollbackTest extends BaseEntityManagerFunctionalTestCase { - - @Override - public Class[] getAnnotatedClasses() { - return new Class[] { - Shipment.class - }; - } - - @Test - @TestForIssue( jiraKey = "HHH-11407") - public void checkRollBackTransactionIsExecutedOnceWhenACommitFails() throws Exception { - EntityManager em = createEntityManager(); - try { - final Session session = em.unwrap( Session.class ); - final OperationCollectorObserver transactionObserver = new OperationCollectorObserver(); - ( (JdbcSessionOwner) session ).getTransactionCoordinator().addObserver( transactionObserver ); - em.getTransaction().begin(); - - // given two inserted records - em.persist( new Shipment( "shipment-1", "INITIAL" ) ); - em.persist( new Shipment( "shipment-2", "INITIAL" ) ); - - em.flush(); - em.clear(); - - try { - // when provoking a duplicate-key exception - em.persist( new Shipment( "shipment-1", "INITIAL" ) ); - em.getTransaction().commit(); - fail( "Expected exception was not raised" ); - } - catch (Exception e) { - // Nothing to do - } - - assertThat( transactionObserver.getUnSuccessfulAfterCompletion(), is( 1 ) ); - - em.clear(); - em.getTransaction().begin(); - - Shipment shipment = em.find( Shipment.class, "shipment-1" ); - if ( shipment != null ) { - em.remove( shipment ); - } - - shipment = em.find( Shipment.class, "shipment-2" ); - if ( shipment != null ) { - em.remove( shipment ); - } - - em.getTransaction().commit(); - } - finally { - em.close(); - } - } - - - @Entity(name = "Shipment") - public class Shipment { - - @Id - private String id; - - @Version - private long version; - - private String state; - - Shipment() { - } - - public Shipment(String id, String state) { - this.id = id; - this.state = state; - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public long getVersion() { - return version; - } - - public void setVersion(long version) { - this.version = version; - } - - public String getState() { - return state; - } - - public void setState(String state) { - this.state = state; - } - - } - - private class OperationCollectorObserver implements TransactionObserver { - int unSuccessfulAfterCompletion; - - @Override - public void afterBegin() { - // Nothing to do - } - - @Override - public void beforeCompletion() { - // Nothing to do - } - - @Override - public void afterCompletion(boolean successful, boolean delayed) { - if ( !successful ) { - unSuccessfulAfterCompletion++; - } - } - - public int getUnSuccessfulAfterCompletion() { - return unSuccessfulAfterCompletion; - } - } - -} diff --git a/hibernate-core/src/test/java/org/hibernate/jpa/test/transaction/CloseEntityManagerWithActiveTransactionTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/transaction/CloseEntityManagerWithActiveTransactionTest.java similarity index 73% rename from hibernate-core/src/test/java/org/hibernate/jpa/test/transaction/CloseEntityManagerWithActiveTransactionTest.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/jpa/transaction/CloseEntityManagerWithActiveTransactionTest.java index bd81641e84..663b2018a6 100644 --- a/hibernate-core/src/test/java/org/hibernate/jpa/test/transaction/CloseEntityManagerWithActiveTransactionTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/transaction/CloseEntityManagerWithActiveTransactionTest.java @@ -4,11 +4,10 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.jpa.test.transaction; +package org.hibernate.orm.test.jpa.transaction; import java.util.HashSet; import java.util.List; -import java.util.Map; import java.util.Set; import javax.persistence.CascadeType; import javax.persistence.Entity; @@ -26,44 +25,48 @@ import javax.transaction.Status; import javax.transaction.TransactionManager; import org.hibernate.Session; +import org.hibernate.cfg.AvailableSettings; import org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl; import org.hibernate.internal.SessionImpl; -import org.hibernate.jpa.AvailableSettings; -import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; +import org.hibernate.jpa.test.transaction.Book; import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.jta.TestingJtaBootstrap; import org.hibernate.testing.jta.TestingJtaPlatformImpl; -import org.junit.After; -import org.junit.Test; +import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; +import org.hibernate.testing.orm.junit.Jpa; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; + +import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.core.Is.is; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * @author Andrea Boriero */ -public class CloseEntityManagerWithActiveTransactionTest extends BaseEntityManagerFunctionalTestCase { - @Override - protected void addConfigOptions(Map options) { - super.addConfigOptions( options ); - TestingJtaBootstrap.prepare( options ); - options.put( AvailableSettings.TRANSACTION_TYPE, "JTA" ); - options.put( org.hibernate.cfg.AvailableSettings.JPA_TRANSACTION_COMPLIANCE, "true" ); - } +@Jpa( + annotatedClasses = { + Book.class, + CloseEntityManagerWithActiveTransactionTest.Container.class, + CloseEntityManagerWithActiveTransactionTest.Box.class, + CloseEntityManagerWithActiveTransactionTest.Muffin.class, + CloseEntityManagerWithActiveTransactionTest.SmallBox.class + }, + integrationSettings = { + @Setting(name = AvailableSettings.CONNECTION_PROVIDER, value = "org.hibernate.testing.jta.JtaAwareConnectionProviderImpl"), + @Setting(name = AvailableSettings.JPA_TRANSACTION_TYPE, value = "JTA"), + @Setting(name = AvailableSettings.JPA_TRANSACTION_COMPLIANCE, value = "true") + }, + nonStringValueSettingProviders = { JtaPlatformNonStringValueSettingProvider.class } +) +public class CloseEntityManagerWithActiveTransactionTest { - @Override - public Class[] getAnnotatedClasses() { - return new Class[] { - Book.class, Container.class, Box.class, Muffin.class, SmallBox.class - }; - } - - @After - public void tearDown() throws Exception { + @AfterEach + public void tearDown(EntityManagerFactoryScope scope) throws Exception { TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin(); - EntityManager em = getOrCreateEntityManager(); + EntityManager em = scope.getEntityManagerFactory().createEntityManager(); try { em.createQuery( "delete from Muffin" ).executeUpdate(); em.createQuery( "delete from Box" ).executeUpdate(); @@ -71,8 +74,8 @@ public class CloseEntityManagerWithActiveTransactionTest extends BaseEntityManag } catch (Exception e) { final TransactionManager transactionManager = TestingJtaPlatformImpl.INSTANCE.getTransactionManager(); - if ( transactionManager.getTransaction() != null && transactionManager.getTransaction() - .getStatus() == Status.STATUS_ACTIVE ) { + if ( transactionManager.getTransaction() != null && + transactionManager.getTransaction().getStatus() == Status.STATUS_ACTIVE ) { TestingJtaPlatformImpl.INSTANCE.getTransactionManager().rollback(); } throw e; @@ -86,9 +89,9 @@ public class CloseEntityManagerWithActiveTransactionTest extends BaseEntityManag @Test @TestForIssue(jiraKey = "HHH-10942") - public void testPersistThenCloseWithAnActiveTransaction() throws Exception { + public void testPersistThenCloseWithAnActiveTransaction(EntityManagerFactoryScope scope) throws Exception { TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin(); - EntityManager em = getOrCreateEntityManager(); + EntityManager em = scope.getEntityManagerFactory().createEntityManager(); try { Box box = new Box(); box.setColor( "red-and-white" ); @@ -109,7 +112,7 @@ public class CloseEntityManagerWithActiveTransactionTest extends BaseEntityManag em.close(); } } - em = getOrCreateEntityManager(); + em = scope.getEntityManagerFactory().createEntityManager(); try { final List results = em.createQuery( "from Box" ).getResultList(); assertThat( results.size(), is( 1 ) ); @@ -120,10 +123,10 @@ public class CloseEntityManagerWithActiveTransactionTest extends BaseEntityManag } @Test - @TestForIssue( jiraKey = "HHH-11166") - public void testMergeThenCloseWithAnActiveTransaction() throws Exception { + @TestForIssue(jiraKey = "HHH-11166") + public void testMergeThenCloseWithAnActiveTransaction(EntityManagerFactoryScope scope) throws Exception { TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin(); - EntityManager em = getOrCreateEntityManager(); + EntityManager em = scope.getEntityManagerFactory().createEntityManager(); try { Box box = new Box(); box.setColor( "red-and-white" ); @@ -132,7 +135,7 @@ public class CloseEntityManagerWithActiveTransactionTest extends BaseEntityManag TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit(); TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin(); - em = getOrCreateEntityManager(); + em = scope.getEntityManagerFactory().createEntityManager(); Muffin muffin = new Muffin(); muffin.setKind( "blueberry" ); @@ -157,7 +160,7 @@ public class CloseEntityManagerWithActiveTransactionTest extends BaseEntityManag em.close(); } } - em = getOrCreateEntityManager(); + em = scope.getEntityManagerFactory().createEntityManager(); try { final List boxes = em.createQuery( "from Box" ).getResultList(); assertThat( boxes.size(), is( 1 ) ); @@ -169,21 +172,21 @@ public class CloseEntityManagerWithActiveTransactionTest extends BaseEntityManag } @Test - @TestForIssue( jiraKey = "HHH-11269") - public void testMergeWithDeletionOrphanRemovalThenCloseWithAnActiveTransaction() throws Exception { + @TestForIssue(jiraKey = "HHH-11269") + public void testMergeWithDeletionOrphanRemovalThenCloseWithAnActiveTransaction(EntityManagerFactoryScope scope) throws Exception { TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin(); - EntityManager em = getOrCreateEntityManager(); + EntityManager em = scope.getEntityManagerFactory().createEntityManager(); try { Muffin muffin = new Muffin(); muffin.setKind( "blueberry" ); - SmallBox box = new SmallBox(muffin); + SmallBox box = new SmallBox( muffin ); box.setColor( "red-and-white" ); em.persist( box ); em.close(); TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit(); TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin(); - em = getOrCreateEntityManager(); + em = scope.getEntityManagerFactory().createEntityManager(); box.emptyBox(); @@ -206,11 +209,11 @@ public class CloseEntityManagerWithActiveTransactionTest extends BaseEntityManag em.close(); } } - em = getOrCreateEntityManager(); + em = scope.getEntityManagerFactory().createEntityManager(); try { final List boxes = em.createQuery( "from SmallBox" ).getResultList(); assertThat( boxes.size(), is( 1 ) ); - assertTrue( boxes.get( 0 ).isEmpty()); + assertTrue( boxes.get( 0 ).isEmpty() ); } finally { em.close(); @@ -218,10 +221,10 @@ public class CloseEntityManagerWithActiveTransactionTest extends BaseEntityManag } @Test - @TestForIssue( jiraKey = "HHH-11166") - public void testUpdateThenCloseWithAnActiveTransaction() throws Exception { + @TestForIssue(jiraKey = "HHH-11166") + public void testUpdateThenCloseWithAnActiveTransaction(EntityManagerFactoryScope scope) throws Exception { TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin(); - EntityManager em = getOrCreateEntityManager(); + EntityManager em = scope.getEntityManagerFactory().createEntityManager(); try { Box box = new Box(); box.setColor( "red-and-white" ); @@ -230,7 +233,7 @@ public class CloseEntityManagerWithActiveTransactionTest extends BaseEntityManag TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit(); TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin(); - em = getOrCreateEntityManager(); + em = scope.getEntityManagerFactory().createEntityManager(); box = em.find( Box.class, box.getId() ); Muffin muffin = new Muffin(); muffin.setKind( "blueberry" ); @@ -252,7 +255,7 @@ public class CloseEntityManagerWithActiveTransactionTest extends BaseEntityManag em.close(); } } - em = getOrCreateEntityManager(); + em = scope.getEntityManagerFactory().createEntityManager(); try { final List boxes = em.createQuery( "from Box" ).getResultList(); assertThat( boxes.size(), is( 1 ) ); @@ -264,10 +267,10 @@ public class CloseEntityManagerWithActiveTransactionTest extends BaseEntityManag } @Test - @TestForIssue( jiraKey = "HHH-11166") - public void testRemoveThenCloseWithAnActiveTransaction() throws Exception { + @TestForIssue(jiraKey = "HHH-11166") + public void testRemoveThenCloseWithAnActiveTransaction(EntityManagerFactoryScope scope) throws Exception { TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin(); - EntityManager em = getOrCreateEntityManager(); + EntityManager em = scope.getEntityManagerFactory().createEntityManager(); try { Box box = new Box(); box.setColor( "red-and-white" ); @@ -279,7 +282,7 @@ public class CloseEntityManagerWithActiveTransactionTest extends BaseEntityManag TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit(); TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin(); - em = getOrCreateEntityManager(); + em = scope.getEntityManagerFactory().createEntityManager(); box = em.find( Box.class, box.getId() ); em.remove( box ); @@ -299,7 +302,7 @@ public class CloseEntityManagerWithActiveTransactionTest extends BaseEntityManag em.close(); } } - em = getOrCreateEntityManager(); + em = scope.getEntityManagerFactory().createEntityManager(); try { final List boxes = em.createQuery( "from Box" ).getResultList(); assertThat( boxes.size(), is( 0 ) ); @@ -311,9 +314,9 @@ public class CloseEntityManagerWithActiveTransactionTest extends BaseEntityManag @Test @TestForIssue(jiraKey = "HHH-11099") - public void testCommitReleasesLogicalConnection() throws Exception { + public void testCommitReleasesLogicalConnection(EntityManagerFactoryScope scope) throws Exception { TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin(); - EntityManager em = getOrCreateEntityManager(); + EntityManager em = scope.getEntityManagerFactory().createEntityManager(); try { Box box = new Box(); box.setColor( "red-and-white" ); @@ -396,30 +399,30 @@ public class CloseEntityManagerWithActiveTransactionTest extends BaseEntityManag @Entity(name = "SmallBox") public static class SmallBox extends Container { - @OneToOne(cascade = {CascadeType.MERGE, + @OneToOne(cascade = { + CascadeType.MERGE, CascadeType.REMOVE, CascadeType.REFRESH, CascadeType.PERSIST }, orphanRemoval = true) private Muffin muffin; - public SmallBox(){} + public SmallBox() { + } public SmallBox(Muffin muffin) { this.muffin = muffin; } - public void emptyBox(){ + public void emptyBox() { muffin = null; } - public boolean isEmpty(){ + public boolean isEmpty() { return muffin == null; } } - - @Entity(name = "Muffin") public static class Muffin { @@ -447,10 +450,4 @@ public class CloseEntityManagerWithActiveTransactionTest extends BaseEntityManag this.kind = kind; } } - - - @FunctionalInterface - public interface Action{ - void execute(); - } } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/transaction/GetTransactionTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/transaction/GetTransactionTest.java index d93a3e21c7..4f6cca436b 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/transaction/GetTransactionTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/transaction/GetTransactionTest.java @@ -6,7 +6,6 @@ */ package org.hibernate.orm.test.jpa.transaction; -import javax.persistence.EntityManager; import javax.persistence.EntityTransaction; import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; @@ -14,9 +13,9 @@ import org.hibernate.testing.orm.junit.Jpa; import org.junit.jupiter.api.Test; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertSame; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertSame; /** * @author Andrea Boriero diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/transaction/JtaGetTransactionThrowsExceptionTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/transaction/JtaGetTransactionThrowsExceptionTest.java index b3725f248e..136a01e949 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/transaction/JtaGetTransactionThrowsExceptionTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/transaction/JtaGetTransactionThrowsExceptionTest.java @@ -8,8 +8,7 @@ package org.hibernate.orm.test.jpa.transaction; import javax.persistence.EntityManager; -import org.hibernate.jpa.AvailableSettings; -import org.hibernate.jpa.test.transaction.Book; +import org.hibernate.cfg.AvailableSettings; import org.hibernate.testing.TestForIssue; import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; @@ -24,11 +23,11 @@ import static org.junit.jupiter.api.Assertions.assertThrows; */ @Jpa( integrationSettings = { - @Setting(name = AvailableSettings.TRANSACTION_TYPE, value = "JTA"), + @Setting(name = AvailableSettings.JPA_TRANSACTION_TYPE, value = "JTA"), @Setting(name = org.hibernate.cfg.AvailableSettings.CONNECTION_PROVIDER, value = "org.hibernate.testing.jta.JtaAwareConnectionProviderImpl"), @Setting( name = org.hibernate.cfg.AvailableSettings.JPA_TRANSACTION_COMPLIANCE, value = "true") }, - nonStringValueSettingProviders = { SynchronizationTypeTest.JtaPlatformNonStringValueSettingProvider.class } + nonStringValueSettingProviders = { JtaPlatformNonStringValueSettingProvider.class } ) public class JtaGetTransactionThrowsExceptionTest { diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/transaction/JtaPlatformNonStringValueSettingProvider.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/transaction/JtaPlatformNonStringValueSettingProvider.java new file mode 100644 index 0000000000..2b638ef899 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/transaction/JtaPlatformNonStringValueSettingProvider.java @@ -0,0 +1,27 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.orm.test.jpa.transaction; + +import org.hibernate.cfg.AvailableSettings; + +import org.hibernate.testing.jta.TestingJtaPlatformImpl; +import org.hibernate.testing.orm.jpa.NonStringValueSettingProvider; + +/** + * @author Jan Schatteman + */ +public class JtaPlatformNonStringValueSettingProvider extends NonStringValueSettingProvider { + @Override + public String getKey() { + return AvailableSettings.JTA_PLATFORM; + } + + @Override + public Object getValue() { + return TestingJtaPlatformImpl.INSTANCE; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/transaction/JtaReusingEntityTransactionTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/transaction/JtaReusingEntityTransactionTest.java new file mode 100644 index 0000000000..c8768c82ca --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/transaction/JtaReusingEntityTransactionTest.java @@ -0,0 +1,73 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.orm.test.jpa.transaction; + +import java.util.List; +import javax.persistence.Entity; +import javax.persistence.EntityTransaction; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; + +import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; +import org.hibernate.testing.orm.junit.Jpa; +import org.hibernate.testing.orm.junit.Setting; + +import org.junit.jupiter.api.Test; + +import static org.hamcrest.core.Is.is; +import static org.hamcrest.MatcherAssert.assertThat; + +/** + * @author Andrea Boriero + */ +@Jpa( + annotatedClasses = { JtaReusingEntityTransactionTest.TestEntity.class }, + integrationSettings = { + @Setting(name = org.hibernate.cfg.AvailableSettings.CONNECTION_PROVIDER, value = "org.hibernate.testing.jta.JtaAwareConnectionProviderImpl"), + @Setting(name = org.hibernate.cfg.AvailableSettings.JPA_TRANSACTION_TYPE, value = "JTA") + }, + nonStringValueSettingProviders = { JtaPlatformNonStringValueSettingProvider.class } +) +public class JtaReusingEntityTransactionTest { + + @Test + public void entityTransactionShouldBeReusableTest(EntityManagerFactoryScope scope) { + scope.inEntityManager( + entityManager -> { + EntityTransaction transaction = null; + try { + transaction = entityManager.getTransaction(); + entityManager.persist( new TestEntity() ); + transaction.begin(); + transaction.commit(); + transaction.begin(); + entityManager.persist( new TestEntity() ); + transaction.commit(); + } + finally { + if ( transaction != null && transaction.isActive() ) { + transaction.rollback(); + } + } + } + ); + + scope.inTransaction( + entityManager -> { + List results = entityManager.createQuery( "from TestEntity" ).getResultList(); + assertThat( results.size(), is( 2 ) ); + } + ); + } + + @Entity(name = "TestEntity") + public static class TestEntity { + @Id + @GeneratedValue + private Long id; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/transaction/SynchronizationTypeTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/transaction/SynchronizationTypeTest.java index 7ccd574d37..eae8096c23 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/transaction/SynchronizationTypeTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/transaction/SynchronizationTypeTest.java @@ -15,15 +15,13 @@ import javax.persistence.criteria.CriteriaUpdate; import org.hibernate.engine.spi.SharedSessionContractImplementor; import org.hibernate.engine.transaction.internal.jta.JtaStatusHelper; -import org.hibernate.jpa.AvailableSettings; +import org.hibernate.cfg.AvailableSettings; import org.hibernate.jpa.test.transaction.Book; import org.hibernate.jpa.test.transaction.Book_; -import org.hibernate.jpa.test.transaction.TransactionJoiningTest; import org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorImpl; import org.hibernate.testing.TestForIssue; import org.hibernate.testing.jta.TestingJtaPlatformImpl; -import org.hibernate.testing.orm.jpa.NonStringValueSettingProvider; import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; import org.hibernate.testing.orm.junit.ExtraAssertions; import org.hibernate.testing.orm.junit.Jpa; @@ -46,26 +44,14 @@ import static org.junit.jupiter.api.Assertions.fail; @Jpa( annotatedClasses = { Book.class }, integrationSettings = { - @Setting(name = AvailableSettings.TRANSACTION_TYPE, value = "JTA"), + @Setting(name = AvailableSettings.JPA_TRANSACTION_TYPE, value = "JTA"), @Setting(name = org.hibernate.cfg.AvailableSettings.CONNECTION_PROVIDER, value = "org.hibernate.testing.jta.JtaAwareConnectionProviderImpl"), }, - nonStringValueSettingProviders = { SynchronizationTypeTest.JtaPlatformNonStringValueSettingProvider.class } + nonStringValueSettingProviders = { JtaPlatformNonStringValueSettingProvider.class } ) public class SynchronizationTypeTest { - public static class JtaPlatformNonStringValueSettingProvider extends NonStringValueSettingProvider { - @Override - public String getKey() { - return org.hibernate.cfg.AvailableSettings.JTA_PLATFORM; - } - - @Override - public Object getValue() { - return TestingJtaPlatformImpl.INSTANCE; - } - } - @Test public void testUnSynchronizedExplicitJoinHandling(EntityManagerFactoryScope scope) throws Exception { diff --git a/hibernate-core/src/test/java/org/hibernate/jpa/test/transaction/TransactionJoiningTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/transaction/TransactionJoiningTest.java similarity index 58% rename from hibernate-core/src/test/java/org/hibernate/jpa/test/transaction/TransactionJoiningTest.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/jpa/transaction/TransactionJoiningTest.java index 566a8fa6ab..07b729d189 100644 --- a/hibernate-core/src/test/java/org/hibernate/jpa/test/transaction/TransactionJoiningTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/transaction/TransactionJoiningTest.java @@ -4,9 +4,8 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.jpa.test.transaction; +package org.hibernate.orm.test.jpa.transaction; -import java.util.Map; import java.util.concurrent.CountDownLatch; import javax.persistence.EntityManager; import javax.persistence.PersistenceException; @@ -18,67 +17,81 @@ import org.hibernate.HibernateException; import org.hibernate.engine.spi.SharedSessionContractImplementor; import org.hibernate.engine.transaction.internal.jta.JtaStatusHelper; import org.hibernate.internal.SessionImpl; -import org.hibernate.jpa.AvailableSettings; -import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; -import org.hibernate.orm.test.jpa.transaction.TransactionJoinHandlingChecker; +import org.hibernate.jpa.test.transaction.Book; import org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorImpl; import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.jta.TestingJtaBootstrap; import org.hibernate.testing.jta.TestingJtaPlatformImpl; -import org.hibernate.testing.junit4.ExtraAssertions; -import org.junit.Test; +import org.hibernate.testing.orm.junit.ExtraAssertions; +import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; +import org.hibernate.testing.orm.junit.Jpa; +import org.hibernate.testing.orm.junit.Setting; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Assertions; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * Largely a copy of {@link org.hibernate.test.jpa.txn.JtaTransactionJoiningTest} * * @author Steve Ebersole */ -public class TransactionJoiningTest extends BaseEntityManagerFunctionalTestCase { - @Override - protected void addConfigOptions(Map options) { - super.addConfigOptions( options ); - TestingJtaBootstrap.prepare( options ); - options.put( AvailableSettings.TRANSACTION_TYPE, "JTA" ); - } +@Jpa( + annotatedClasses = { + Book.class + }, + integrationSettings = { + @Setting(name = org.hibernate.cfg.AvailableSettings.CONNECTION_PROVIDER, value = "org.hibernate.testing.jta.JtaAwareConnectionProviderImpl"), + @Setting(name = org.hibernate.cfg.AvailableSettings.JPA_TRANSACTION_TYPE, value = "JTA") + }, + nonStringValueSettingProviders = { JtaPlatformNonStringValueSettingProvider.class } +) +public class TransactionJoiningTest { @Test - public void testExplicitJoining() throws Exception { + public void testExplicitJoining(EntityManagerFactoryScope scope) throws Exception { assertFalse( JtaStatusHelper.isActive( TestingJtaPlatformImpl.INSTANCE.getTransactionManager() ) ); - EntityManager entityManager = entityManagerFactory().createEntityManager( SynchronizationType.UNSYNCHRONIZED ); - TransactionJoinHandlingChecker.validateExplicitJoiningHandling( entityManager ); + EntityManager entityManager = scope.getEntityManagerFactory() + .createEntityManager( SynchronizationType.UNSYNCHRONIZED ); + try { + TransactionJoinHandlingChecker.validateExplicitJoiningHandling( entityManager ); + } + finally { + entityManager.close(); + } } @Test - @SuppressWarnings("EmptyCatchBlock") - public void testExplicitJoiningTransactionRequiredException() throws Exception { + public void testExplicitJoiningTransactionRequiredException(EntityManagerFactoryScope scope) { // explicitly calling EntityManager#joinTransaction outside of an active transaction should cause // a TransactionRequiredException to be thrown - EntityManager entityManager = entityManagerFactory().createEntityManager(); - assertFalse("setup problem", JtaStatusHelper.isActive(TestingJtaPlatformImpl.INSTANCE.getTransactionManager())); + EntityManager entityManager = scope.getEntityManagerFactory().createEntityManager(); + assertFalse( JtaStatusHelper.isActive( TestingJtaPlatformImpl.INSTANCE.getTransactionManager() ), "setup problem" ); try { - entityManager.joinTransaction(); - fail( "Expected joinTransaction() to fail since there is no active JTA transaction" ); + Assertions.assertThrows( + TransactionRequiredException.class, + entityManager::joinTransaction, + "Expected joinTransaction() to fail since there is no active JTA transaction" + ); } - catch (TransactionRequiredException expected) { + finally { + entityManager.close(); } } @Test - public void testImplicitJoining() throws Exception { + public void testImplicitJoining(EntityManagerFactoryScope scope) throws Exception { // here the transaction is started before the EM is opened... assertFalse( JtaStatusHelper.isActive( TestingJtaPlatformImpl.INSTANCE.getTransactionManager() ) ); TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin(); - EntityManager entityManager = entityManagerFactory().createEntityManager(); + EntityManager entityManager = scope.getEntityManagerFactory().createEntityManager(); SharedSessionContractImplementor session = entityManager.unwrap( SharedSessionContractImplementor.class ); ExtraAssertions.assertTyping( JtaTransactionCoordinatorImpl.class, session.getTransactionCoordinator() ); @@ -101,11 +114,11 @@ public class TransactionJoiningTest extends BaseEntityManagerFunctionalTestCase @Test @TestForIssue(jiraKey = "HHH-10807") - public void testIsJoinedAfterMarkedForRollbackImplict() throws Exception { + public void testIsJoinedAfterMarkedForRollbackImplicit(EntityManagerFactoryScope scope) throws Exception { assertFalse( JtaStatusHelper.isActive( TestingJtaPlatformImpl.INSTANCE.getTransactionManager() ) ); TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin(); - EntityManager entityManager = entityManagerFactory().createEntityManager(); + EntityManager entityManager = scope.getEntityManagerFactory().createEntityManager(); SharedSessionContractImplementor session = entityManager.unwrap( SharedSessionContractImplementor.class ); ExtraAssertions.assertTyping( JtaTransactionCoordinatorImpl.class, session.getTransactionCoordinator() ); @@ -123,20 +136,25 @@ public class TransactionJoiningTest extends BaseEntityManagerFunctionalTestCase assertTrue( transactionCoordinator.isJoined() ); assertTrue( entityManager.isJoinedToTransaction() ); - TestingJtaPlatformImpl.INSTANCE.getTransactionManager().rollback(); - - entityManager.close(); - assertFalse( entityManager.isOpen() ); - assertFalse( session.isOpen() ); + try { + TestingJtaPlatformImpl.INSTANCE.getTransactionManager().rollback(); + entityManager.close(); + assertFalse( entityManager.isOpen() ); + assertFalse( session.isOpen() ); + } + finally { + // ensure the entityManager is closed in case the rollback call fails + entityManager.close(); + } } @Test @TestForIssue(jiraKey = "HHH-10807") - public void testIsJoinedAfterMarkedForRollbackExplicit() throws Exception { + public void testIsJoinedAfterMarkedForRollbackExplicit(EntityManagerFactoryScope scope) throws Exception { assertFalse( JtaStatusHelper.isActive( TestingJtaPlatformImpl.INSTANCE.getTransactionManager() ) ); - EntityManager entityManager = entityManagerFactory().createEntityManager( SynchronizationType.UNSYNCHRONIZED ); + EntityManager entityManager = scope.getEntityManagerFactory().createEntityManager( SynchronizationType.UNSYNCHRONIZED ); SharedSessionContractImplementor session = entityManager.unwrap( SharedSessionContractImplementor.class ); assertTrue( entityManager.isOpen() ); assertTrue( session.isOpen() ); @@ -157,19 +175,25 @@ public class TransactionJoiningTest extends BaseEntityManagerFunctionalTestCase assertTrue( transactionCoordinator.isJoined() ); assertTrue( entityManager.isJoinedToTransaction() ); - TestingJtaPlatformImpl.INSTANCE.getTransactionManager().rollback(); + try { + TestingJtaPlatformImpl.INSTANCE.getTransactionManager().rollback(); - entityManager.close(); - assertFalse( entityManager.isOpen() ); - assertFalse( session.isOpen() ); + entityManager.close(); + assertFalse( entityManager.isOpen() ); + assertFalse( session.isOpen() ); + } + finally { + // ensure the entityManager is closed in case the rollback call fails + entityManager.close(); + } } @Test - public void testCloseAfterCommit() throws Exception { + public void testCloseAfterCommit(EntityManagerFactoryScope scope) throws Exception { assertFalse( JtaStatusHelper.isActive( TestingJtaPlatformImpl.INSTANCE.getTransactionManager() ) ); TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin(); - EntityManager entityManager = entityManagerFactory().createEntityManager(); + EntityManager entityManager = scope.getEntityManagerFactory().createEntityManager(); SharedSessionContractImplementor session = entityManager.unwrap( SharedSessionContractImplementor.class ); ExtraAssertions.assertTyping( JtaTransactionCoordinatorImpl.class, session.getTransactionCoordinator() ); @@ -181,21 +205,27 @@ public class TransactionJoiningTest extends BaseEntityManagerFunctionalTestCase assertTrue( entityManager.isOpen() ); assertTrue( session.isOpen() ); - TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit(); - assertTrue( entityManager.isOpen() ); - assertTrue( session.isOpen() ); + try { + TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit(); + assertTrue( entityManager.isOpen() ); + assertTrue( session.isOpen() ); - entityManager.close(); - assertFalse( entityManager.isOpen() ); - assertFalse( session.isOpen() ); + entityManager.close(); + assertFalse( entityManager.isOpen() ); + assertFalse( session.isOpen() ); + } + finally { + // ensure the entityManager is closed in case the commit call fails + entityManager.close(); + } } @Test - public void testImplicitJoiningWithExtraSynchronization() throws Exception { + public void testImplicitJoiningWithExtraSynchronization(EntityManagerFactoryScope scope) throws Exception { assertFalse( JtaStatusHelper.isActive( TestingJtaPlatformImpl.INSTANCE.getTransactionManager() ) ); TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin(); - EntityManager entityManager = entityManagerFactory().createEntityManager(); + EntityManager entityManager = scope.getEntityManagerFactory().createEntityManager(); SharedSessionContractImplementor session = entityManager.unwrap( SharedSessionContractImplementor.class ); ExtraAssertions.assertTyping( JtaTransactionCoordinatorImpl.class, session.getTransactionCoordinator() ); @@ -209,7 +239,7 @@ public class TransactionJoiningTest extends BaseEntityManagerFunctionalTestCase TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit(); } - + /** * In certain JTA environments (JBossTM, etc.), a background thread (reaper) * can rollback a transaction if it times out. These timeouts are rare and @@ -220,55 +250,51 @@ public class TransactionJoiningTest extends BaseEntityManagerFunctionalTestCase */ @Test @TestForIssue(jiraKey = "HHH-7910") - public void testMultiThreadTransactionTimeout() throws Exception { + public void testMultiThreadTransactionTimeout(EntityManagerFactoryScope scope) throws Exception { TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin(); - EntityManager em = entityManagerFactory().createEntityManager(); - final SessionImpl sImpl = em.unwrap( SessionImpl.class ); + EntityManager em = scope.getEntityManagerFactory().createEntityManager(); + try { + final SessionImpl sImpl = em.unwrap( SessionImpl.class ); - final CountDownLatch latch = new CountDownLatch( 1 ); + final CountDownLatch latch = new CountDownLatch( 1 ); - Thread thread = new Thread() { - public void run() { - ((JtaTransactionCoordinatorImpl)sImpl.getTransactionCoordinator()).getSynchronizationCallbackCoordinator() + Thread thread = new Thread( () -> { + ( (JtaTransactionCoordinatorImpl) sImpl.getTransactionCoordinator() ).getSynchronizationCallbackCoordinator() .afterCompletion( Status.STATUS_ROLLEDBACK ); latch.countDown(); + } ); + thread.start(); + + latch.await(); + + boolean caught = false; + try { + em.persist( new Book( "The Book of Foo", 1 ) ); } - }; - thread.start(); + catch (PersistenceException e) { + caught = e.getCause().getClass().equals( HibernateException.class ); + } + assertTrue( caught ); - latch.await(); + // Ensure that the connection was closed by the background thread. + caught = false; + try { + em.createQuery( "from Book" ).getResultList(); + } + catch (PersistenceException e) { + // HHH-9312 + caught = true; + } + catch (Exception e) { + caught = true; + } + assertTrue( caught ); - boolean caught = false; - try { - em.persist( new Book( "The Book of Foo", 1 ) ); + TestingJtaPlatformImpl.INSTANCE.getTransactionManager().rollback(); } - catch ( PersistenceException e ) { - caught = e.getCause().getClass().equals( HibernateException.class ); + finally { + em.close(); } - assertTrue( caught ); - - // Ensure that the connection was closed by the background thread. - caught = false; - try { - em.createQuery( "from Book" ).getResultList(); - } - catch ( PersistenceException e ) { - // HHH-9312 - caught = true; - }catch (Exception e){ - caught = true; - } - assertTrue( caught ); - - TestingJtaPlatformImpl.INSTANCE.getTransactionManager().rollback(); - em.close(); - } - - @Override - public Class[] getAnnotatedClasses() { - return new Class[] { - Book.class - }; } } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/transaction/TransactionRollbackTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/transaction/TransactionRollbackTest.java new file mode 100644 index 0000000000..60b2afd82e --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/transaction/TransactionRollbackTest.java @@ -0,0 +1,161 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.orm.test.jpa.transaction; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Version; + +import org.hibernate.Session; +import org.hibernate.engine.transaction.spi.TransactionObserver; +import org.hibernate.resource.jdbc.spi.JdbcSessionOwner; + +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; +import org.hibernate.testing.orm.junit.Jpa; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import static org.hamcrest.core.Is.is; +import static org.hamcrest.MatcherAssert.assertThat; + +/** + * @author Andrea Boriero + */ +@Jpa(annotatedClasses = { + TransactionRollbackTest.Shipment.class +}) +public class TransactionRollbackTest { + + @AfterEach + public void tearDown(EntityManagerFactoryScope scope) { + scope.inTransaction( + entityManager -> { + entityManager.createQuery( "delete from Shipment" ).executeUpdate(); + } + ); + } + + @Test + @TestForIssue( jiraKey = "HHH-11407") + public void checkRollBackTransactionIsExecutedOnceWhenACommitFails(EntityManagerFactoryScope scope) { + scope.inEntityManager( + entityManager -> { + final Session session = entityManager.unwrap( Session.class ); + final OperationCollectorObserver transactionObserver = new OperationCollectorObserver(); + ( (JdbcSessionOwner) session ).getTransactionCoordinator().addObserver( transactionObserver ); + entityManager.getTransaction().begin(); + + // given two inserted records + entityManager.persist( new Shipment( "shipment-1", "INITIAL" ) ); + entityManager.persist( new Shipment( "shipment-2", "INITIAL" ) ); + + entityManager.flush(); + entityManager.clear(); + + Assertions.assertThrows( + Exception.class, + () -> { + // when provoking a duplicate-key exception + entityManager.persist( new Shipment( "shipment-1", "INITIAL" ) ); + entityManager.getTransaction().commit(); + }, + "Expected exception was not raised" + ); + + assertThat( transactionObserver.getUnSuccessfulAfterCompletion(), is( 1 ) ); + + entityManager.clear(); + entityManager.getTransaction().begin(); + + Shipment shipment = entityManager.find( Shipment.class, "shipment-1" ); + if ( shipment != null ) { + entityManager.remove( shipment ); + } + + shipment = entityManager.find( Shipment.class, "shipment-2" ); + if ( shipment != null ) { + entityManager.remove( shipment ); + } + + entityManager.getTransaction().commit(); + } + ); + } + + @Entity(name = "Shipment") + public class Shipment { + + @Id + private String id; + + @Version + private long version; + + private String state; + + Shipment() { + } + + public Shipment(String id, String state) { + this.id = id; + this.state = state; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public long getVersion() { + return version; + } + + public void setVersion(long version) { + this.version = version; + } + + public String getState() { + return state; + } + + public void setState(String state) { + this.state = state; + } + + } + + private class OperationCollectorObserver implements TransactionObserver { + int unSuccessfulAfterCompletion; + + @Override + public void afterBegin() { + // Nothing to do + } + + @Override + public void beforeCompletion() { + // Nothing to do + } + + @Override + public void afterCompletion(boolean successful, boolean delayed) { + if ( !successful ) { + unSuccessfulAfterCompletion++; + } + } + + public int getUnSuccessfulAfterCompletion() { + return unSuccessfulAfterCompletion; + } + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/jpa/test/transaction/TransactionRolledBackInDifferentThreadTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/transaction/TransactionRolledBackInDifferentThreadTest.java similarity index 61% rename from hibernate-core/src/test/java/org/hibernate/jpa/test/transaction/TransactionRolledBackInDifferentThreadTest.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/jpa/transaction/TransactionRolledBackInDifferentThreadTest.java index fd88c25448..6ce26f24ff 100644 --- a/hibernate-core/src/test/java/org/hibernate/jpa/test/transaction/TransactionRolledBackInDifferentThreadTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/transaction/TransactionRolledBackInDifferentThreadTest.java @@ -4,25 +4,23 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.jpa.test.transaction; +package org.hibernate.orm.test.jpa.transaction; import javax.persistence.EntityManager; import javax.transaction.RollbackException; import javax.transaction.Status; import javax.transaction.SystemException; -import java.util.Map; import org.hibernate.HibernateException; -import org.hibernate.jpa.AvailableSettings; -import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; -import org.junit.Ignore; -import org.junit.Test; +import org.junit.jupiter.api.Test; -import org.hibernate.testing.jta.TestingJtaBootstrap; import org.hibernate.testing.jta.TestingJtaPlatformImpl; +import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; +import org.hibernate.testing.orm.junit.Jpa; +import org.hibernate.testing.orm.junit.Setting; -import static org.junit.Assert.fail; +import static org.junit.jupiter.api.Assertions.fail; /** * Recreate test failure that occurs when three threads share the same entity manager and @@ -30,18 +28,19 @@ import static org.junit.Assert.fail; * * @author Scott Marlow */ -public class TransactionRolledBackInDifferentThreadTest extends BaseEntityManagerFunctionalTestCase { - @Override - protected void addConfigOptions(Map options) { - super.addConfigOptions( options ); - TestingJtaBootstrap.prepare( options ); - options.put( AvailableSettings.TRANSACTION_TYPE, "JTA" ); - } +@Jpa( + integrationSettings = { + @Setting(name = org.hibernate.cfg.AvailableSettings.CONNECTION_PROVIDER, value = "org.hibernate.testing.jta.JtaAwareConnectionProviderImpl"), + @Setting(name = org.hibernate.cfg.AvailableSettings.JPA_TRANSACTION_TYPE, value = "JTA") + }, + nonStringValueSettingProviders = { JtaPlatformNonStringValueSettingProvider.class } +) +public class TransactionRolledBackInDifferentThreadTest { @Test - public void testTransactionRolledBackInDifferentThreadFailure() throws Exception { + public void testTransactionRolledBackInDifferentThreadFailure(EntityManagerFactoryScope scope) throws Exception { - /** + /* * The three test threads share the same entity manager. * The main test thread creates an EntityManager, joins it to the transaction and ends the transaction. * Test thread 1 joins the EntityManager to its transaction, sets rollbackonly and ends the transaction. @@ -51,18 +50,18 @@ public class TransactionRolledBackInDifferentThreadTest extends BaseEntityManage // main test thread TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin(); - final EntityManager em = entityManagerFactory().createEntityManager(); + final EntityManager em = scope.getEntityManagerFactory().createEntityManager(); em.joinTransaction(); - TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit(); - // will be set to the failing exception - final HibernateException[] transactionRolledBackInDifferentThreadException = new HibernateException[2]; - transactionRolledBackInDifferentThreadException[0] = transactionRolledBackInDifferentThreadException[1] = null; + try { + TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit(); - // background test thread 1 - final Runnable run1 = new Runnable() { - @Override - public void run() { + // will be set to the failing exception + final HibernateException[] transactionRolledBackInDifferentThreadException = new HibernateException[2]; + transactionRolledBackInDifferentThreadException[0] = transactionRolledBackInDifferentThreadException[1] = null; + + // background test thread 1 + final Runnable run1 = () -> { try { TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin(); em.joinTransaction(); @@ -72,7 +71,7 @@ public class TransactionRolledBackInDifferentThreadTest extends BaseEntityManage catch (javax.persistence.PersistenceException e) { if ( e.getCause() instanceof HibernateException && e.getCause().getMessage().equals( "Transaction was rolled back in a different thread!" ) ) { - /** + /* * Save the exception for the main test thread to fail */ e.printStackTrace(); // show the error first @@ -81,7 +80,6 @@ public class TransactionRolledBackInDifferentThreadTest extends BaseEntityManage } catch (RollbackException ignored) { // expected to see RollbackException: ARJUNA016053: Could not commit transaction. - } catch (Throwable throwable) { throwable.printStackTrace(); @@ -94,20 +92,15 @@ public class TransactionRolledBackInDifferentThreadTest extends BaseEntityManage } } catch (SystemException ignore) { - } } + }; - } - }; - - // test thread 2 - final Runnable run2 = new Runnable() { - @Override - public void run() { + // test thread 2 + final Runnable run2 = () -> { try { TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin(); - /** + /* * the following call to em.joinTransaction() will throw: * org.hibernate.HibernateException: Transaction was rolled back in a different thread! */ @@ -117,7 +110,7 @@ public class TransactionRolledBackInDifferentThreadTest extends BaseEntityManage catch (javax.persistence.PersistenceException e) { if ( e.getCause() instanceof HibernateException && e.getCause().getMessage().equals( "Transaction was rolled back in a different thread!" ) ) { - /** + /* * Save the exception for the main test thread to fail */ e.printStackTrace(); // show the error first @@ -135,45 +128,36 @@ public class TransactionRolledBackInDifferentThreadTest extends BaseEntityManage } } catch (SystemException ignore) { - } } + }; + + Thread thread = new Thread( run1, "test thread1" ); + thread.start(); + thread.join(); + + Thread thread2 = new Thread( run2, "test thread2" ); + thread2.start(); + thread2.join(); + + // show failure for exception caught in run2.run() + if ( transactionRolledBackInDifferentThreadException[0] != null + || transactionRolledBackInDifferentThreadException[1] != null ) + { + fail( + "failure in test thread 1 = " + + ( transactionRolledBackInDifferentThreadException[0] != null ? + transactionRolledBackInDifferentThreadException[0].getMessage() : + "(none)" ) + + ", failure in test thread 2 = " + + ( transactionRolledBackInDifferentThreadException[1] != null ? + transactionRolledBackInDifferentThreadException[1].getMessage() : + "(none)" ) + ); } - }; - - Thread thread = new Thread( run1, "test thread1" ); - thread.start(); - thread.join(); - - Thread thread2 = new Thread( run2, "test thread2" ); - thread2.start(); - thread2.join(); - - // show failure for exception caught in run2.run() - if ( transactionRolledBackInDifferentThreadException[0] != null - || transactionRolledBackInDifferentThreadException[1] != null ) - - { - fail( - "failure in test thread 1 = " + - ( transactionRolledBackInDifferentThreadException[0] != null ? - transactionRolledBackInDifferentThreadException[0].getMessage() : - "(none)" ) - + ", failure in test thread 2 = " + - ( transactionRolledBackInDifferentThreadException[1] != null ? - transactionRolledBackInDifferentThreadException[1].getMessage() : - "(none)" ) - ); } - - em.close(); - } - - - @Override - public Class[] getAnnotatedClasses() { - return new Class[] { - - }; + finally { + em.close(); + } } } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/transaction/batch/AbstractJtaBatchTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/transaction/batch/AbstractJtaBatchTest.java index 5e89f84e4e..7b160b050a 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/transaction/batch/AbstractJtaBatchTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/transaction/batch/AbstractJtaBatchTest.java @@ -63,18 +63,6 @@ public abstract class AbstractJtaBatchTest { } ); } - public static class JtaPlatformNonStringValueSettingProvider extends NonStringValueSettingProvider { - @Override - public String getKey() { - return AvailableSettings.JTA_PLATFORM; - } - - @Override - public Object getValue() { - return TestingJtaPlatformImpl.INSTANCE; - } - } - public static class ConnectionNonStringValueSettingProvider extends NonStringValueSettingProvider { @Override public String getKey() { diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/transaction/batch/JtaWithFailingBatchTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/transaction/batch/JtaWithFailingBatchTest.java index a5ce50d6a7..64f24a81af 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/transaction/batch/JtaWithFailingBatchTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/transaction/batch/JtaWithFailingBatchTest.java @@ -21,6 +21,7 @@ import org.hibernate.engine.jdbc.batch.internal.BatchingBatch; import org.hibernate.engine.jdbc.batch.spi.Batch; import org.hibernate.engine.jdbc.batch.spi.BatchKey; import org.hibernate.engine.jdbc.spi.JdbcCoordinator; +import org.hibernate.orm.test.jpa.transaction.JtaPlatformNonStringValueSettingProvider; import org.hibernate.testing.TestForIssue; import org.hibernate.testing.jta.TestingJtaPlatformImpl; @@ -55,7 +56,7 @@ import static org.junit.jupiter.api.Assertions.assertFalse; @Setting(name = AvailableSettings.STATEMENT_BATCH_SIZE, value = "50") }, nonStringValueSettingProviders = { - AbstractJtaBatchTest.JtaPlatformNonStringValueSettingProvider.class, + JtaPlatformNonStringValueSettingProvider.class, AbstractJtaBatchTest.ConnectionNonStringValueSettingProvider.class, JtaWithFailingBatchTest.BatchBuilderNonStringValueSettingProvider.class } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/transaction/batch/JtaWithStatementsBatchTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/transaction/batch/JtaWithStatementsBatchTest.java index e3e63177d7..0d2d629d74 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/transaction/batch/JtaWithStatementsBatchTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/transaction/batch/JtaWithStatementsBatchTest.java @@ -10,9 +10,7 @@ import java.sql.PreparedStatement; import java.util.ArrayList; import java.util.List; import javax.persistence.FlushModeType; -import javax.transaction.NotSupportedException; import javax.transaction.Status; -import javax.transaction.SystemException; import javax.transaction.TransactionManager; import org.hibernate.cfg.AvailableSettings; @@ -22,6 +20,7 @@ import org.hibernate.engine.jdbc.batch.internal.BatchingBatch; import org.hibernate.engine.jdbc.batch.spi.Batch; import org.hibernate.engine.jdbc.batch.spi.BatchKey; import org.hibernate.engine.jdbc.spi.JdbcCoordinator; +import org.hibernate.orm.test.jpa.transaction.JtaPlatformNonStringValueSettingProvider; import org.hibernate.testing.TestForIssue; import org.hibernate.testing.jta.TestingJtaPlatformImpl; @@ -55,7 +54,7 @@ import static org.junit.jupiter.api.Assertions.assertFalse; @Setting(name = AvailableSettings.STATEMENT_BATCH_SIZE, value = "50") }, nonStringValueSettingProviders = { - AbstractJtaBatchTest.JtaPlatformNonStringValueSettingProvider.class, + JtaPlatformNonStringValueSettingProvider.class, AbstractJtaBatchTest.ConnectionNonStringValueSettingProvider.class, JtaWithStatementsBatchTest.BatchBuilderNonStringValueSettingProvider.class }