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) {
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.putAll( serviceRegistry.getService( ConfigurationService.class ).getSettings() );
@ -522,6 +515,14 @@ public final class SessionFactoryImpl
this.transactionEnvironment = new TransactionEnvironmentImpl( this );
this.observer.sessionFactoryCreated( this );
SessionFactoryRegistry.INSTANCE.addSessionFactory(
uuid,
name,
settings.isSessionFactoryNameAlsoJndiName(),
this,
serviceRegistry.getService( JndiService.class )
);
}
private void applyCfgXmlValues(LoadedConfig aggregatedConfig, SessionFactoryServiceRegistry serviceRegistry) {

View File

@ -6,16 +6,20 @@ import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
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.Test;
import org.hibernate.MappingException;
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;
import static org.junit.Assert.assertFalse;
/**
* @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( "discriminator value '" + DISCRIMINATOR_VALUE + "'." ) );
}
assertFalse( SessionFactoryRegistry.INSTANCE.hasRegistrations() );
}
@Test
@ -45,23 +51,19 @@ public class DuplicatedDiscriminatorValueTest extends BaseUnitTestCase {
}
private void tryBuildingSessionFactory(Class... annotatedClasses) {
Configuration cfg = new Configuration();
for ( Class annotatedClass : annotatedClasses ) {
cfg.addAnnotatedClass( annotatedClass );
}
ServiceRegistry serviceRegistry = null;
SessionFactory sessionFactory = null;
final StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().build();
try {
serviceRegistry = ServiceRegistryBuilder.buildServiceRegistry( cfg.getProperties() );
sessionFactory = cfg.buildSessionFactory( serviceRegistry );
final MetadataSources metadataSources = new MetadataSources( serviceRegistry );
for ( Class annotatedClass : annotatedClasses ) {
metadataSources.addAnnotatedClass( annotatedClass );
}
finally {
if ( sessionFactory != null ) {
final Metadata metadata = metadataSources.buildMetadata();
final SessionFactory sessionFactory = metadata.buildSessionFactory();
sessionFactory.close();
}
if ( serviceRegistry != null ) {
ServiceRegistryBuilder.destroy( serviceRegistry );
}
finally {
StandardServiceRegistryBuilder.destroy( serviceRegistry );
}
}

View File

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