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
*/
protected final void initialize(boolean writing) {
if ( !initialized ) {
if ( initializing ) {
throw new LazyInitializationException( "illegal access to loading collection" );
protected final void initialize(final boolean writing) {
if ( initialized ) {
return;
}
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() {

View File

@ -21,22 +21,19 @@
* 51 Franklin Street, Fifth Floor
* 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.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import java.math.BigDecimal;
import org.hibernate.annotations.GenericGenerator;
@Entity
@Table(name = "O_CUSTINVENTORY")
public class Inventory {
private int id = -1;
private Store store;

View File

@ -20,27 +20,26 @@
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.hibernate.test.annotations.derivedidentities.e1.b.specjmapid.ondemand;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
package org.hibernate.test.ondemandload;
import java.math.BigDecimal;
import java.util.List;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.After;
import org.junit.Before;
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 {
@Before
@ -96,6 +95,21 @@ public class LazyLoadingTest extends BaseCoreFunctionalTestCase {
assertEquals( 2, sessionFactory().getStatistics().getSessionOpenCount() );
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.close();
}
@ -156,10 +170,6 @@ public class LazyLoadingTest extends BaseCoreFunctionalTestCase {
assertEquals( 2, sessionFactory().getStatistics().getSessionCloseCount() );
}
public LazyLoadingTest() {
System.setProperty( "hibernate.enable_specj_proprietary_syntax", "true" );
}
@Override
protected void configure(Configuration cfg) {
super.configure( cfg );
@ -169,6 +179,11 @@ public class LazyLoadingTest extends BaseCoreFunctionalTestCase {
@Override
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
* 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.Id;

View File

@ -21,7 +21,7 @@
* 51 Franklin Street, Fifth Floor
* 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.Column;