Skip to content

Commit

Permalink
HHH-10952 - Tests leaving transactions opened cause PostgreSQL to hang
Browse files Browse the repository at this point in the history
  • Loading branch information
dreab8 committed Jul 13, 2016
1 parent a17dd12 commit e406236
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 118 deletions.
Expand Up @@ -30,19 +30,27 @@ public void prepare() {
cfg.setProperty( Environment.USE_SECOND_LEVEL_CACHE, "false" );
super.prepare( cfg );

Session s = getFactory().openSession();
s.beginTransaction();

Store store = new Store( 1 ).setName( "Acme Super Outlet" );
s.persist( store );

Product product = new Product( "007" ).setName( "widget" ).setDescription( "FooBar" );
s.persist( product );

store.addInventoryProduct( product ).setQuantity( 10L ).setStorePrice( new BigDecimal( 500 ) );

s.getTransaction().commit();
s.close();
try (Session s = getFactory().openSession()) {
s.beginTransaction();
try {

Store store = new Store( 1 ).setName( "Acme Super Outlet" );
s.persist( store );

Product product = new Product( "007" ).setName( "widget" ).setDescription( "FooBar" );
s.persist( product );

store.addInventoryProduct( product ).setQuantity( 10L ).setStorePrice( new BigDecimal( 500 ) );

s.getTransaction().commit();
}
catch (Exception e) {
if ( s.getTransaction().isActive() ) {
s.getTransaction().rollback();
}
throw e;
}
}
}

public void cleanup() {
Expand All @@ -51,57 +59,64 @@ public void cleanup() {
public void execute() {
getFactory().getStatistics().clear();

Session s = getFactory().openSession();
s.beginTransaction();

// 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() );

// 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() );

// 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();
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() );

// 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() );

// 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() );

s.getTransaction().commit();
}
catch (Exception e) {
if ( s.getTransaction().isActive() ) {
s.getTransaction().rollback();
}
throw e;
}
}
}

protected void configure(Configuration cfg) {
Expand Down
Expand Up @@ -31,41 +31,56 @@ public void prepare() {
super.prepare( cfg );


Session s = getFactory().openSession();
s.beginTransaction();

Store store = new Store( 1 ).setName( "Acme Super Outlet" );
s.persist( store );

Product product = new Product( "007" ).setName( "widget" ).setDescription( "FooBar" );
s.persist( product );

store.addInventoryProduct( product ).setQuantity( 10L ).setStorePrice( new BigDecimal( 500 ) );

s.getTransaction().commit();
s.close();
try(Session s = getFactory().openSession()) {
s.beginTransaction();
try {
Store store = new Store( 1 ).setName( "Acme Super Outlet" );
s.persist( store );

Product product = new Product( "007" ).setName( "widget" ).setDescription( "FooBar" );
s.persist( product );

store.addInventoryProduct( product ).setQuantity( 10L ).setStorePrice( new BigDecimal( 500 ) );

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();

// 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() );

// close the session and try to initialize collection
s.getTransaction().commit();
s.close();
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() ) );

assertEquals( 1, getFactory().getStatistics().getSessionOpenCount() );
assertEquals( 0, getFactory().getStatistics().getSessionCloseCount() );

// 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() );
Expand Down
Expand Up @@ -30,41 +30,56 @@ public void prepare() {
cfg.setProperty( Environment.USE_SECOND_LEVEL_CACHE, "false" );
super.prepare( cfg );

Session s = getFactory().openSession();
s.beginTransaction();

Store store = new Store( 1 ).setName( "Acme Super Outlet" );
s.persist( store );

Product product = new Product( "007" ).setName( "widget" ).setDescription( "FooBar" );
s.persist( product );

store.addInventoryProduct( product ).setQuantity( 10L ).setStorePrice( new BigDecimal( 500 ) );

s.getTransaction().commit();
s.close();
try (Session s = getFactory().openSession()) {
s.beginTransaction();
try {
Store store = new Store( 1 ).setName( "Acme Super Outlet" );
s.persist( store );

Product product = new Product( "007" ).setName( "widget" ).setDescription( "FooBar" );
s.persist( product );

store.addInventoryProduct( product ).setQuantity( 10L ).setStorePrice( new BigDecimal( 500 ) );

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();

// 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() );

// close the session and try to initialize store
s.getTransaction().commit();
s.close();
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 ) );

assertEquals( 1, getFactory().getStatistics().getSessionOpenCount() );
assertEquals( 0, getFactory().getStatistics().getSessionCloseCount() );

// 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() );
Expand Down

0 comments on commit e406236

Please sign in to comment.