From e4062362acefad4a7aec0973a60aca750fcc85e8 Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Wed, 13 Jul 2016 10:16:37 +0200 Subject: [PATCH] HHH-10952 - Tests leaving transactions opened cause PostgreSQL to hang --- ...yCollectionWithClearedSessionTestTask.java | 125 ++++++++++-------- ...zyCollectionWithClosedSessionTestTask.java | 59 ++++++--- ...ntityLoadingWithClosedSessionTestTask.java | 59 ++++++--- 3 files changed, 144 insertions(+), 99 deletions(-) diff --git a/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/ondemandload/LazyCollectionWithClearedSessionTestTask.java b/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/ondemandload/LazyCollectionWithClearedSessionTestTask.java index ad5591a13f..4c6942421d 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/ondemandload/LazyCollectionWithClearedSessionTestTask.java +++ b/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/ondemandload/LazyCollectionWithClearedSessionTestTask.java @@ -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) { diff --git a/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/ondemandload/LazyCollectionWithClosedSessionTestTask.java b/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/ondemandload/LazyCollectionWithClosedSessionTestTask.java index 6545ec28a1..f3a844602d 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/ondemandload/LazyCollectionWithClosedSessionTestTask.java +++ b/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/ondemandload/LazyCollectionWithClosedSessionTestTask.java @@ -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() ); diff --git a/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/ondemandload/LazyEntityLoadingWithClosedSessionTestTask.java b/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/ondemandload/LazyEntityLoadingWithClosedSessionTestTask.java index b0109a9534..32c52d0a6a 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/ondemandload/LazyEntityLoadingWithClosedSessionTestTask.java +++ b/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/ondemandload/LazyEntityLoadingWithClosedSessionTestTask.java @@ -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() );