HHH-9655 - Add logging to help better track down tests that leave SessionFactories open

This commit is contained in:
Steve Ebersole 2015-03-10 20:44:10 -05:00
parent 83756008d2
commit b318d21cc8
3 changed files with 38 additions and 30 deletions

View File

@ -241,13 +241,6 @@ public final class SessionFactoryImpl
catch (Exception e) { catch (Exception e) {
throw new AssertionFailure("Could not generate UUID"); throw new AssertionFailure("Could not generate UUID");
} }
SessionFactoryRegistry.INSTANCE.addSessionFactory(
uuid,
name,
settings.isSessionFactoryNameAlsoJndiName(),
this,
serviceRegistry.getService( JndiService.class )
);
this.properties = new Properties(); this.properties = new Properties();
this.properties.putAll( serviceRegistry.getService( ConfigurationService.class ).getSettings() ); this.properties.putAll( serviceRegistry.getService( ConfigurationService.class ).getSettings() );
@ -522,6 +515,14 @@ public final class SessionFactoryImpl
this.transactionEnvironment = new TransactionEnvironmentImpl( this ); this.transactionEnvironment = new TransactionEnvironmentImpl( this );
this.observer.sessionFactoryCreated( this ); this.observer.sessionFactoryCreated( this );
SessionFactoryRegistry.INSTANCE.addSessionFactory(
uuid,
name,
settings.isSessionFactoryNameAlsoJndiName(),
this,
serviceRegistry.getService( JndiService.class )
);
} }
private void applyCfgXmlValues(LoadedConfig aggregatedConfig, SessionFactoryServiceRegistry serviceRegistry) { private void applyCfgXmlValues(LoadedConfig aggregatedConfig, SessionFactoryServiceRegistry serviceRegistry) {

View File

@ -6,16 +6,20 @@ import javax.persistence.Entity;
import javax.persistence.GeneratedValue; import javax.persistence.GeneratedValue;
import javax.persistence.Id; import javax.persistence.Id;
import org.hibernate.MappingException;
import org.hibernate.SessionFactory;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.internal.SessionFactoryRegistry;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseUnitTestCase;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import org.hibernate.MappingException; import static org.junit.Assert.assertFalse;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.testing.ServiceRegistryBuilder;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseUnitTestCase;
/** /**
* @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com) * @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com)
@ -37,6 +41,8 @@ public class DuplicatedDiscriminatorValueTest extends BaseUnitTestCase {
Assert.assertTrue( errorMsg.contains( Building2.class.getName() ) ); Assert.assertTrue( errorMsg.contains( Building2.class.getName() ) );
Assert.assertTrue( errorMsg.contains( "discriminator value '" + DISCRIMINATOR_VALUE + "'." ) ); Assert.assertTrue( errorMsg.contains( "discriminator value '" + DISCRIMINATOR_VALUE + "'." ) );
} }
assertFalse( SessionFactoryRegistry.INSTANCE.hasRegistrations() );
} }
@Test @Test
@ -45,23 +51,19 @@ public class DuplicatedDiscriminatorValueTest extends BaseUnitTestCase {
} }
private void tryBuildingSessionFactory(Class... annotatedClasses) { private void tryBuildingSessionFactory(Class... annotatedClasses) {
Configuration cfg = new Configuration(); final StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().build();
for ( Class annotatedClass : annotatedClasses ) {
cfg.addAnnotatedClass( annotatedClass );
}
ServiceRegistry serviceRegistry = null;
SessionFactory sessionFactory = null;
try { try {
serviceRegistry = ServiceRegistryBuilder.buildServiceRegistry( cfg.getProperties() ); final MetadataSources metadataSources = new MetadataSources( serviceRegistry );
sessionFactory = cfg.buildSessionFactory( serviceRegistry ); for ( Class annotatedClass : annotatedClasses ) {
metadataSources.addAnnotatedClass( annotatedClass );
}
final Metadata metadata = metadataSources.buildMetadata();
final SessionFactory sessionFactory = metadata.buildSessionFactory();
sessionFactory.close();
} }
finally { finally {
if ( sessionFactory != null ) { StandardServiceRegistryBuilder.destroy( serviceRegistry );
sessionFactory.close();
}
if ( serviceRegistry != null ) {
ServiceRegistryBuilder.destroy( serviceRegistry );
}
} }
} }

View File

@ -20,18 +20,19 @@
*/ */
package org.hibernate.test.cfg.persister; package org.hibernate.test.cfg.persister;
import org.junit.Test;
import org.hibernate.MappingException; import org.hibernate.MappingException;
import org.hibernate.SessionFactory; import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Configuration;
import org.hibernate.internal.SessionFactoryRegistry;
import org.hibernate.persister.spi.PersisterClassResolver; import org.hibernate.persister.spi.PersisterClassResolver;
import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistry;
import org.hibernate.testing.junit4.BaseUnitTestCase; import org.hibernate.testing.junit4.BaseUnitTestCase;
import org.junit.Test;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
/** /**
@ -73,6 +74,8 @@ public class PersisterClassProviderTest extends BaseUnitTestCase {
StandardServiceRegistryBuilder.destroy( serviceRegistry ); StandardServiceRegistryBuilder.destroy( serviceRegistry );
} }
assertFalse( SessionFactoryRegistry.INSTANCE.hasRegistrations() );
cfg = new Configuration(); cfg = new Configuration();
cfg.addAnnotatedClass( Portal.class ); cfg.addAnnotatedClass( Portal.class );
cfg.addAnnotatedClass( Window.class ); cfg.addAnnotatedClass( Window.class );
@ -117,5 +120,7 @@ public class PersisterClassProviderTest extends BaseUnitTestCase {
finally { finally {
StandardServiceRegistryBuilder.destroy( serviceRegistry ); StandardServiceRegistryBuilder.destroy( serviceRegistry );
} }
assertFalse( SessionFactoryRegistry.INSTANCE.hasRegistrations() );
} }
} }