HHH-7524 - Enabling AvailableSettings.ENABLE_LAZY_LOAD_NO_TRANS results in leaking DB-connections

(cherry picked from commit 52336f4150)
This commit is contained in:
Steve Ebersole 2012-08-16 11:27:59 -05:00
parent 0c0f951715
commit cde9ee1572
5 changed files with 101 additions and 94 deletions

View File

@ -512,25 +512,20 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
* *
* @throws LazyInitializationException if we cannot initialize * @throws LazyInitializationException if we cannot initialize
*/ */
protected final void initialize(boolean writing) { protected final void initialize(final boolean writing) {
if ( !initialized ) { if ( initialized ) {
if ( initializing ) { return;
throw new LazyInitializationException( "illegal access to loading collection" );
}
else if ( session == null ) {
throw new LazyInitializationException( "could not initialize proxy - no Session" );
}
else if ( !session.isOpen() ) {
throw new LazyInitializationException( "could not initialize proxy - the owning Session was closed" );
}
else if ( !session.isConnected() ) {
throw new LazyInitializationException( "could not initialize proxy - the owning Session is disconnected" );
}
else {
throwLazyInitializationExceptionIfNotConnected();
session.initializeCollection( this, writing );
}
} }
withTemporarySessionIfNeeded(
new LazyInitializationWork<Object>() {
@Override
public Object doWork() {
session.initializeCollection( AbstractPersistentCollection.this, writing );
return null;
}
}
);
} }
private void throwLazyInitializationExceptionIfNotConnected() { private void throwLazyInitializationExceptionIfNotConnected() {

View File

@ -21,22 +21,19 @@
* 51 Franklin Street, Fifth Floor * 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA * Boston, MA 02110-1301 USA
*/ */
package org.hibernate.test.annotations.derivedidentities.e1.b.specjmapid.ondemand; package org.hibernate.test.ondemandload;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.GeneratedValue; import javax.persistence.GeneratedValue;
import javax.persistence.Id; import javax.persistence.Id;
import javax.persistence.JoinColumn; import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne; import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import java.math.BigDecimal; import java.math.BigDecimal;
import org.hibernate.annotations.GenericGenerator; import org.hibernate.annotations.GenericGenerator;
@Entity @Entity
@Table(name = "O_CUSTINVENTORY")
public class Inventory { public class Inventory {
private int id = -1; private int id = -1;
private Store store; private Store store;

View File

@ -1,4 +1,4 @@
/* /*
* JBoss, Home of Professional Open Source * JBoss, Home of Professional Open Source
* Copyright 2012, JBoss Inc., and individual contributors as indicated * Copyright 2012, JBoss Inc., and individual contributors as indicated
* by the @authors tag. See the copyright.txt in the distribution for a * by the @authors tag. See the copyright.txt in the distribution for a
@ -20,85 +20,99 @@
* 02110-1301 USA, or see the FSF site: http://www.fsf.org. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/ */
package org.hibernate.test.annotations.derivedidentities.e1.b.specjmapid.ondemand; package org.hibernate.test.ondemandload;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.List;
import org.hibernate.Hibernate; import org.hibernate.Hibernate;
import org.hibernate.Session; import org.hibernate.Session;
import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment; import org.hibernate.cfg.Environment;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
public class LazyLoadingTest extends BaseCoreFunctionalTestCase { public class LazyLoadingTest extends BaseCoreFunctionalTestCase {
@Before @Before
public void setUpData() { public void setUpData() {
Session s = openSession(); Session s = openSession();
s.beginTransaction(); s.beginTransaction();
Store store = new Store( 1 ) Store store = new Store( 1 )
.setName( "Acme Super Outlet" ); .setName( "Acme Super Outlet" );
s.persist( store ); s.persist( store );
Product product = new Product( "007" ) Product product = new Product( "007" )
.setName( "widget" ) .setName( "widget" )
.setDescription( "FooBar" ); .setDescription( "FooBar" );
s.persist( product ); s.persist( product );
store.addInventoryProduct( product ) store.addInventoryProduct( product )
.setQuantity( 10L ) .setQuantity( 10L )
.setStorePrice( new BigDecimal( 500 ) ); .setStorePrice( new BigDecimal( 500 ) );
s.getTransaction().commit(); s.getTransaction().commit();
s.close(); s.close();
} }
@After @After
public void cleanUpData() { public void cleanUpData() {
Session s = openSession(); Session s = openSession();
s.beginTransaction(); s.beginTransaction();
s.delete( s.get( Store.class, 1 ) ); s.delete( s.get( Store.class, 1 ) );
s.delete( s.get( Product.class, "007" ) ); s.delete( s.get( Product.class, "007" ) );
s.getTransaction().commit(); s.getTransaction().commit();
s.close(); s.close();
} }
@Test @Test
public void testLazyCollectionLoadingWithClearedSession() { public void testLazyCollectionLoadingWithClearedSession() {
sessionFactory().getStatistics().clear(); sessionFactory().getStatistics().clear();
Session s = openSession(); Session s = openSession();
s.beginTransaction(); s.beginTransaction();
// first load the store, making sure collection is not initialized // first load the store, making sure collection is not initialized
Store store = (Store)s.get( Store.class, 1 ); Store store = (Store) s.get( Store.class, 1 );
assertNotNull( store ); assertNotNull( store );
assertFalse( Hibernate.isInitialized( store.getInventories() ) ); assertFalse( Hibernate.isInitialized( store.getInventories() ) );
assertEquals( 1, sessionFactory().getStatistics().getSessionOpenCount() ); assertEquals( 1, sessionFactory().getStatistics().getSessionOpenCount() );
assertEquals( 0, sessionFactory().getStatistics().getSessionCloseCount() ); assertEquals( 0, sessionFactory().getStatistics().getSessionCloseCount() );
// then clear session and try to initialize collection // then clear session and try to initialize collection
s.clear(); s.clear();
store.getInventories().size(); store.getInventories().size();
assertTrue( Hibernate.isInitialized( store.getInventories() ) ); assertTrue( Hibernate.isInitialized( store.getInventories() ) );
assertEquals( 2, sessionFactory().getStatistics().getSessionOpenCount() ); assertEquals( 2, sessionFactory().getStatistics().getSessionOpenCount() );
assertEquals( 1, sessionFactory().getStatistics().getSessionCloseCount() ); assertEquals( 1, sessionFactory().getStatistics().getSessionCloseCount() );
s.getTransaction().commit(); s.clear();
s.close(); store = (Store) s.get( Store.class, 1 );
} assertNotNull( store );
assertFalse( Hibernate.isInitialized( store.getInventories() ) );
assertEquals( 2, sessionFactory().getStatistics().getSessionOpenCount() );
assertEquals( 1, sessionFactory().getStatistics().getSessionCloseCount() );
s.clear();
store.getInventories().iterator();
assertTrue( Hibernate.isInitialized( store.getInventories() ) );
assertEquals( 3, sessionFactory().getStatistics().getSessionOpenCount() );
assertEquals( 2, sessionFactory().getStatistics().getSessionCloseCount() );
s.getTransaction().commit();
s.close();
}
@Test @Test
public void testLazyCollectionLoadingWithClosedSession() { public void testLazyCollectionLoadingWithClosedSession() {
@ -107,7 +121,7 @@ public class LazyLoadingTest extends BaseCoreFunctionalTestCase {
Session s = openSession(); Session s = openSession();
s.beginTransaction(); s.beginTransaction();
// first load the store, making sure collection is not initialized // first load the store, making sure collection is not initialized
Store store = (Store)s.get( Store.class, 1 ); Store store = (Store) s.get( Store.class, 1 );
assertNotNull( store ); assertNotNull( store );
assertFalse( Hibernate.isInitialized( store.getInventories() ) ); assertFalse( Hibernate.isInitialized( store.getInventories() ) );
@ -156,19 +170,20 @@ public class LazyLoadingTest extends BaseCoreFunctionalTestCase {
assertEquals( 2, sessionFactory().getStatistics().getSessionCloseCount() ); assertEquals( 2, sessionFactory().getStatistics().getSessionCloseCount() );
} }
public LazyLoadingTest() { @Override
System.setProperty( "hibernate.enable_specj_proprietary_syntax", "true" ); protected void configure(Configuration cfg) {
super.configure( cfg );
cfg.setProperty( Environment.ENABLE_LAZY_LOAD_NO_TRANS, "true" );
cfg.setProperty( Environment.GENERATE_STATISTICS, "true" );
} }
@Override @Override
protected void configure(Configuration cfg) { protected Class[] getAnnotatedClasses() {
super.configure( cfg ); return new Class[] {
cfg.setProperty( Environment.ENABLE_LAZY_LOAD_NO_TRANS, "true" ); Store.class,
cfg.setProperty( Environment.GENERATE_STATISTICS, "true" ); Inventory.class,
} Product.class
};
}
@Override }
protected Class[] getAnnotatedClasses() {
return new Class[] { Store.class, Inventory.class, Product.class };
}
}

View File

@ -21,7 +21,7 @@
* 51 Franklin Street, Fifth Floor * 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA * Boston, MA 02110-1301 USA
*/ */
package org.hibernate.test.annotations.derivedidentities.e1.b.specjmapid.ondemand; package org.hibernate.test.ondemandload;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.Id; import javax.persistence.Id;

View File

@ -21,7 +21,7 @@
* 51 Franklin Street, Fifth Floor * 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA * Boston, MA 02110-1301 USA
*/ */
package org.hibernate.test.annotations.derivedidentities.e1.b.specjmapid.ondemand; package org.hibernate.test.ondemandload;
import javax.persistence.CascadeType; import javax.persistence.CascadeType;
import javax.persistence.Column; import javax.persistence.Column;