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;
@ -23,13 +24,13 @@ 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 );
final Top top = new Top();
;
TransactionUtil.doInJPA( this::entityManagerFactory, entityManager -> {
entityManager.persist( top );
// Flush 1
em.flush();
entityManager.flush();
Middle middle = new Middle( 1l );
top.addMiddle( middle );
@ -45,23 +46,20 @@ public class MultiLevelCascadeTest extends BaseEntityManagerFunctionalTestCase {
middle2.setBottom( bottom2 );
bottom2.setMiddle( middle2 );
// Flush 2
em.flush();
tx.commit();
em.close();
entityManager.flush();
} );
em = getOrCreateEntityManager();
tx = em.getTransaction();
tx.begin();
TransactionUtil.doInJPA( this::entityManagerFactory, entityManager -> {
top = em.find(Top.class, top.getId());
Top finded = entityManager.find( Top.class, top.getId() );
assertEquals(2, top.getMiddles().size());
for (Middle loadedMiddle : top.getMiddles()) {
assertSame(top, loadedMiddle.getTop());
assertEquals( 2, finded.getMiddles().size() );
for ( Middle loadedMiddle : finded.getMiddles() ) {
assertSame( finded, loadedMiddle.getTop() );
assertNotNull( loadedMiddle.getBottom() );
}
em.remove( top );
em.close();
entityManager.remove( finded );
} );
}
@Override

View File

@ -47,8 +47,13 @@ public class OneToOneOrphanTest extends BaseEntityManagerFunctionalTestCase {
fail( "should have raised an IllegalStateException" );
}
catch (IllegalStateException ex) {
if ( em.getTransaction().isActive() ) {
em.getTransaction().rollback();
}
// IllegalStateException caught as expected
}
finally {
em.close();
}
}
}