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" ); withTemporarySessionIfNeeded(
} new LazyInitializationWork<Object>() {
else if ( !session.isOpen() ) { @Override
throw new LazyInitializationException( "could not initialize proxy - the owning Session was closed" ); public Object doWork() {
} session.initializeCollection( AbstractPersistentCollection.this, writing );
else if ( !session.isConnected() ) { return null;
throw new LazyInitializationException( "could not initialize proxy - the owning Session is disconnected" );
}
else {
throwLazyInitializationExceptionIfNotConnected();
session.initializeCollection( this, writing );
} }
} }
);
} }
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,27 +20,26 @@
* 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
@ -81,7 +80,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() ) );
@ -96,6 +95,21 @@ public class LazyLoadingTest extends BaseCoreFunctionalTestCase {
assertEquals( 2, sessionFactory().getStatistics().getSessionOpenCount() ); assertEquals( 2, sessionFactory().getStatistics().getSessionOpenCount() );
assertEquals( 1, sessionFactory().getStatistics().getSessionCloseCount() ); assertEquals( 1, sessionFactory().getStatistics().getSessionCloseCount() );
s.clear();
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.getTransaction().commit();
s.close(); s.close();
} }
@ -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,10 +170,6 @@ public class LazyLoadingTest extends BaseCoreFunctionalTestCase {
assertEquals( 2, sessionFactory().getStatistics().getSessionCloseCount() ); assertEquals( 2, sessionFactory().getStatistics().getSessionCloseCount() );
} }
public LazyLoadingTest() {
System.setProperty( "hibernate.enable_specj_proprietary_syntax", "true" );
}
@Override @Override
protected void configure(Configuration cfg) { protected void configure(Configuration cfg) {
super.configure( cfg ); super.configure( cfg );
@ -169,6 +179,11 @@ public class LazyLoadingTest extends BaseCoreFunctionalTestCase {
@Override @Override
protected Class[] getAnnotatedClasses() { protected Class[] getAnnotatedClasses() {
return new Class[] { Store.class, Inventory.class, Product.class }; 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;