HHH-10952 - Tests leaving transactions opened cause PostgreSQL to hang

This commit is contained in:
Andrea Boriero 2016-08-03 21:37:27 +02:00
parent 109410b153
commit f7cc256a1c
2 changed files with 44 additions and 41 deletions

View File

@ -13,6 +13,7 @@ import org.junit.Test;
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.transaction.TransactionUtil;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
@ -22,47 +23,44 @@ public class MultiLevelCascadeTest extends BaseEntityManagerFunctionalTestCase {
@TestForIssue( jiraKey = "HHH-5299" )
@Test
public void test() {
EntityManager em = getOrCreateEntityManager( );
EntityTransaction tx = em.getTransaction();
tx.begin();
Top top = new Top();
em.persist( top );
// Flush 1
em.flush();
Middle middle = new Middle( 1l );
top.addMiddle( middle );
middle.setTop( top );
Bottom bottom = new Bottom();
middle.setBottom( bottom );
bottom.setMiddle( middle );
Middle middle2 = new Middle( 2l );
top.addMiddle(middle2);
middle2.setTop( top );
Bottom bottom2 = new Bottom();
middle2.setBottom( bottom2 );
bottom2.setMiddle( middle2 );
// Flush 2
em.flush();
tx.commit();
em.close();
public void test() {
final Top top = new Top();
;
TransactionUtil.doInJPA( this::entityManagerFactory, entityManager -> {
em = getOrCreateEntityManager();
tx = em.getTransaction();
tx.begin();
entityManager.persist( top );
// Flush 1
entityManager.flush();
top = em.find(Top.class, top.getId());
Middle middle = new Middle( 1l );
top.addMiddle( middle );
middle.setTop( top );
Bottom bottom = new Bottom();
middle.setBottom( bottom );
bottom.setMiddle( middle );
assertEquals(2, top.getMiddles().size());
for (Middle loadedMiddle : top.getMiddles()) {
assertSame(top, loadedMiddle.getTop());
assertNotNull(loadedMiddle.getBottom());
}
em.remove( top );
em.close();
}
Middle middle2 = new Middle( 2l );
top.addMiddle( middle2 );
middle2.setTop( top );
Bottom bottom2 = new Bottom();
middle2.setBottom( bottom2 );
bottom2.setMiddle( middle2 );
// Flush 2
entityManager.flush();
} );
TransactionUtil.doInJPA( this::entityManagerFactory, entityManager -> {
Top finded = entityManager.find( Top.class, top.getId() );
assertEquals( 2, finded.getMiddles().size() );
for ( Middle loadedMiddle : finded.getMiddles() ) {
assertSame( finded, loadedMiddle.getTop() );
assertNotNull( loadedMiddle.getBottom() );
}
entityManager.remove( finded );
} );
}
@Override
protected Class[] getAnnotatedClasses() {

View File

@ -41,14 +41,19 @@ public class OneToOneOrphanTest extends BaseEntityManagerFunctionalTestCase {
a.setB(b);
try {
em.persist(a);
em.persist( a );
em.flush();
em.getTransaction().commit();
fail("should have raised an IllegalStateException");
fail( "should have raised an IllegalStateException" );
}
catch (IllegalStateException ex) {
if ( em.getTransaction().isActive() ) {
em.getTransaction().rollback();
}
// IllegalStateException caught as expected
}
em.close();
finally {
em.close();
}
}
}