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

This commit is contained in:
Andrea Boriero 2016-07-13 10:16:37 +02:00
parent a17dd125e7
commit e4062362ac
3 changed files with 144 additions and 99 deletions

View File

@ -30,19 +30,27 @@ public class LazyCollectionWithClearedSessionTestTask extends AbstractEnhancerTe
cfg.setProperty( Environment.USE_SECOND_LEVEL_CACHE, "false" );
super.prepare( cfg );
Session s = getFactory().openSession();
s.beginTransaction();
try (Session s = getFactory().openSession()) {
s.beginTransaction();
try {
Store store = new Store( 1 ).setName( "Acme Super Outlet" );
s.persist( store );
Store store = new Store( 1 ).setName( "Acme Super Outlet" );
s.persist( store );
Product product = new Product( "007" ).setName( "widget" ).setDescription( "FooBar" );
s.persist( product );
Product product = new Product( "007" ).setName( "widget" ).setDescription( "FooBar" );
s.persist( product );
store.addInventoryProduct( product ).setQuantity( 10L ).setStorePrice( new BigDecimal( 500 ) );
store.addInventoryProduct( product ).setQuantity( 10L ).setStorePrice( new BigDecimal( 500 ) );
s.getTransaction().commit();
s.close();
s.getTransaction().commit();
}
catch (Exception e) {
if ( s.getTransaction().isActive() ) {
s.getTransaction().rollback();
}
throw e;
}
}
}
public void cleanup() {
@ -51,57 +59,64 @@ public class LazyCollectionWithClearedSessionTestTask extends AbstractEnhancerTe
public void execute() {
getFactory().getStatistics().clear();
Session s = getFactory().openSession();
s.beginTransaction();
try (Session s = getFactory().openSession()) {
s.beginTransaction();
try {
// first load the store, making sure collection is not initialized
Store store = s.get( Store.class, 1 );
assertNotNull( store );
assertFalse( Hibernate.isPropertyInitialized( store, "inventories" ) );
assertEquals( 1, getFactory().getStatistics().getSessionOpenCount() );
assertEquals( 0, getFactory().getStatistics().getSessionCloseCount() );
// first load the store, making sure collection is not initialized
Store store = s.get( Store.class, 1 );
assertNotNull( store );
assertFalse( Hibernate.isPropertyInitialized( store, "inventories" ) );
assertEquals( 1, getFactory().getStatistics().getSessionOpenCount() );
assertEquals( 0, getFactory().getStatistics().getSessionCloseCount() );
// then clear session and try to initialize collection
s.clear();
assertNotNull( store );
assertFalse( Hibernate.isPropertyInitialized( store, "inventories" ) );
store.getInventories().size();
assertTrue( Hibernate.isPropertyInitialized( store, "inventories" ) );
// the extra Session is the temp Session needed to perform the init
assertEquals( 2, getFactory().getStatistics().getSessionOpenCount() );
assertEquals( 1, getFactory().getStatistics().getSessionCloseCount() );
// then clear session and try to initialize collection
s.clear();
assertNotNull( store );
assertFalse( Hibernate.isPropertyInitialized( store, "inventories" ) );
store.getInventories().size();
assertTrue( Hibernate.isPropertyInitialized( store, "inventories" ) );
// the extra Session is the temp Session needed to perform the init
assertEquals( 2, getFactory().getStatistics().getSessionOpenCount() );
assertEquals( 1, getFactory().getStatistics().getSessionCloseCount() );
// clear Session again. The collection should still be recognized as initialized from above
s.clear();
assertNotNull( store );
assertTrue( Hibernate.isPropertyInitialized( store, "inventories" ) );
assertEquals( 2, getFactory().getStatistics().getSessionOpenCount() );
assertEquals( 1, getFactory().getStatistics().getSessionCloseCount() );
// clear Session again. The collection should still be recognized as initialized from above
s.clear();
assertNotNull( store );
assertTrue( Hibernate.isPropertyInitialized( store, "inventories" ) );
assertEquals( 2, getFactory().getStatistics().getSessionOpenCount() );
assertEquals( 1, getFactory().getStatistics().getSessionCloseCount() );
// lets clear the Session again and this time reload the Store
s.clear();
store = s.get( Store.class, 1 );
s.clear();
assertNotNull( store );
// collection should be back to uninitialized since we have a new entity instance
assertFalse( Hibernate.isPropertyInitialized( store, "inventories" ) );
assertEquals( 2, getFactory().getStatistics().getSessionOpenCount() );
assertEquals( 1, getFactory().getStatistics().getSessionCloseCount() );
store.getInventories().size();
assertTrue( Hibernate.isPropertyInitialized( store, "inventories" ) );
// the extra Session is the temp Session needed to perform the init
assertEquals( 3, getFactory().getStatistics().getSessionOpenCount() );
assertEquals( 2, getFactory().getStatistics().getSessionCloseCount() );
// lets clear the Session again and this time reload the Store
s.clear();
store = s.get( Store.class, 1 );
s.clear();
assertNotNull( store );
// collection should be back to uninitialized since we have a new entity instance
assertFalse( Hibernate.isPropertyInitialized( store, "inventories" ) );
assertEquals( 2, getFactory().getStatistics().getSessionOpenCount() );
assertEquals( 1, getFactory().getStatistics().getSessionCloseCount() );
store.getInventories().size();
assertTrue( Hibernate.isPropertyInitialized( store, "inventories" ) );
// the extra Session is the temp Session needed to perform the init
assertEquals( 3, getFactory().getStatistics().getSessionOpenCount() );
assertEquals( 2, getFactory().getStatistics().getSessionCloseCount() );
// clear Session again. The collection should still be recognized as initialized from above
s.clear();
assertNotNull( store );
assertTrue( Hibernate.isPropertyInitialized( store, "inventories" ) );
assertEquals( 3, getFactory().getStatistics().getSessionOpenCount() );
assertEquals( 2, getFactory().getStatistics().getSessionCloseCount() );
// clear Session again. The collection should still be recognized as initialized from above
s.clear();
assertNotNull( store );
assertTrue( Hibernate.isPropertyInitialized( store, "inventories" ) );
assertEquals( 3, getFactory().getStatistics().getSessionOpenCount() );
assertEquals( 2, getFactory().getStatistics().getSessionCloseCount() );
s.getTransaction().commit();
s.close();
s.getTransaction().commit();
}
catch (Exception e) {
if ( s.getTransaction().isActive() ) {
s.getTransaction().rollback();
}
throw e;
}
}
}
protected void configure(Configuration cfg) {

View File

@ -31,41 +31,56 @@ public class LazyCollectionWithClosedSessionTestTask extends AbstractEnhancerTes
super.prepare( cfg );
Session s = getFactory().openSession();
s.beginTransaction();
try(Session s = getFactory().openSession()) {
s.beginTransaction();
try {
Store store = new Store( 1 ).setName( "Acme Super Outlet" );
s.persist( store );
Store store = new Store( 1 ).setName( "Acme Super Outlet" );
s.persist( store );
Product product = new Product( "007" ).setName( "widget" ).setDescription( "FooBar" );
s.persist( product );
Product product = new Product( "007" ).setName( "widget" ).setDescription( "FooBar" );
s.persist( product );
store.addInventoryProduct( product ).setQuantity( 10L ).setStorePrice( new BigDecimal( 500 ) );
store.addInventoryProduct( product ).setQuantity( 10L ).setStorePrice( new BigDecimal( 500 ) );
s.getTransaction().commit();
s.close();
s.getTransaction().commit();
}
catch (Exception e) {
if ( s.getTransaction().isActive() ) {
s.getTransaction().rollback();
}
throw e;
}
}
}
public void cleanup() {
}
public void execute() {
Store store = null;
getFactory().getStatistics().clear();
Session s = getFactory().openSession();
s.beginTransaction();
try (Session s = getFactory().openSession()) {
s.beginTransaction();
try {
// first load the store, making sure collection is not initialized
store = s.get( Store.class, 1 );
assertNotNull( store );
assertFalse( Hibernate.isInitialized( store.getInventories() ) );
// first load the store, making sure collection is not initialized
Store store = s.get( Store.class, 1 );
assertNotNull( store );
assertFalse( Hibernate.isInitialized( store.getInventories() ) );
assertEquals( 1, getFactory().getStatistics().getSessionOpenCount() );
assertEquals( 0, getFactory().getStatistics().getSessionCloseCount() );
assertEquals( 1, getFactory().getStatistics().getSessionOpenCount() );
assertEquals( 0, getFactory().getStatistics().getSessionCloseCount() );
// close the session and try to initialize collection
s.getTransaction().commit();
s.close();
// close the session and try to initialize collection
s.getTransaction().commit();
}
catch (Exception e) {
if ( s.getTransaction().isActive() ) {
s.getTransaction().rollback();
}
throw e;
}
}
assertEquals( 1, getFactory().getStatistics().getSessionOpenCount() );
assertEquals( 1, getFactory().getStatistics().getSessionCloseCount() );

View File

@ -30,41 +30,56 @@ public class LazyEntityLoadingWithClosedSessionTestTask extends AbstractEnhancer
cfg.setProperty( Environment.USE_SECOND_LEVEL_CACHE, "false" );
super.prepare( cfg );
Session s = getFactory().openSession();
s.beginTransaction();
try (Session s = getFactory().openSession()) {
s.beginTransaction();
try {
Store store = new Store( 1 ).setName( "Acme Super Outlet" );
s.persist( store );
Store store = new Store( 1 ).setName( "Acme Super Outlet" );
s.persist( store );
Product product = new Product( "007" ).setName( "widget" ).setDescription( "FooBar" );
s.persist( product );
Product product = new Product( "007" ).setName( "widget" ).setDescription( "FooBar" );
s.persist( product );
store.addInventoryProduct( product ).setQuantity( 10L ).setStorePrice( new BigDecimal( 500 ) );
store.addInventoryProduct( product ).setQuantity( 10L ).setStorePrice( new BigDecimal( 500 ) );
s.getTransaction().commit();
s.close();
s.getTransaction().commit();
}
catch (Exception e) {
if ( s.getTransaction().isActive() ) {
s.getTransaction().rollback();
}
throw e;
}
}
}
public void cleanup() {
}
public void execute() {
Store store = null;
getFactory().getStatistics().clear();
Session s = getFactory().openSession();
s.beginTransaction();
try (Session s = getFactory().openSession()) {
s.beginTransaction();
try {
// first load the store, making sure it is not initialized
store = s.load( Store.class, 1 );
assertNotNull( store );
assertFalse( Hibernate.isInitialized( store ) );
// first load the store, making sure it is not initialized
Store store = s.load( Store.class, 1 );
assertNotNull( store );
assertFalse( Hibernate.isInitialized( store ) );
assertEquals( 1, getFactory().getStatistics().getSessionOpenCount() );
assertEquals( 0, getFactory().getStatistics().getSessionCloseCount() );
assertEquals( 1, getFactory().getStatistics().getSessionOpenCount() );
assertEquals( 0, getFactory().getStatistics().getSessionCloseCount() );
// close the session and try to initialize store
s.getTransaction().commit();
s.close();
// close the session and try to initialize store
s.getTransaction().commit();
}
catch (Exception e) {
if ( s.getTransaction().isActive() ) {
s.getTransaction().rollback();
}
throw e;
}
}
assertEquals( 1, getFactory().getStatistics().getSessionOpenCount() );
assertEquals( 1, getFactory().getStatistics().getSessionCloseCount() );