HHH-6052 - Make statistics a service

This commit is contained in:
Steve Ebersole 2011-03-28 12:40:26 -05:00
parent 634d9186a5
commit 339faabb14
5 changed files with 144 additions and 15 deletions

View File

@ -193,7 +193,6 @@ public final class SessionFactoryImpl
private final transient UpdateTimestampsCache updateTimestampsCache;
private final transient Map<String,QueryCache> queryCaches;
private final transient ConcurrentMap<String,Region> allCacheRegions = new ConcurrentHashMap<String, Region>();
private final transient Statistics statistics;
private final transient CurrentSessionContext currentSessionContext;
private final transient EntityNotFoundDelegate entityNotFoundDelegate;
private final transient SQLFunctionRegistry sqlFunctionRegistry;
@ -214,18 +213,16 @@ public final class SessionFactoryImpl
SessionFactoryObserver observer) throws HibernateException {
LOG.buildingSessionFactory();
// todo : move stats building to SF service reg building
this.statistics = buildStatistics( settings, serviceRegistry );
this.settings = settings;
this.interceptor = cfg.getInterceptor();
this.properties = new Properties();
this.properties.putAll( cfg.getProperties() );
this.interceptor = cfg.getInterceptor();
this.serviceRegistry = serviceRegistry.getService( SessionFactoryServiceRegistryFactory.class ).buildServiceRegistry(
this,
cfg
);
this.settings = settings;
this.sqlFunctionRegistry = new SQLFunctionRegistry( getDialect(), cfg.getSqlFunctions() );
if ( observer != null ) {
this.observer.addObserver( observer );
@ -536,13 +533,6 @@ public final class SessionFactoryImpl
this.observer.addObserver( observer );
}
private Statistics buildStatistics(Settings settings, ServiceRegistry serviceRegistry) {
Statistics statistics = new ConcurrentStatisticsImpl( this );
statistics.setStatisticsEnabled( settings.isStatisticsEnabled() );
LOG.debugf("Statistics initialized [enabled=%s]", settings.isStatisticsEnabled());
return statistics;
}
public TransactionEnvironment getTransactionEnvironment() {
return transactionEnvironment;
}
@ -1235,11 +1225,11 @@ public final class SessionFactoryImpl
}
public Statistics getStatistics() {
return statistics;
return getStatisticsImplementor();
}
public StatisticsImplementor getStatisticsImplementor() {
return (StatisticsImplementor) statistics;
return serviceRegistry.getService( StatisticsImplementor.class );
}
public FilterDefinition getFilterDefinition(String filterName) throws HibernateException {

View File

@ -28,6 +28,7 @@ import java.util.List;
import org.hibernate.service.event.internal.EventListenerServiceInitiator;
import org.hibernate.service.spi.SessionFactoryServiceInitiator;
import org.hibernate.stat.internal.StatisticsInitiator;
/**
* @author Steve Ebersole
@ -39,6 +40,7 @@ public class StandardSessionFactoryServiceInitiators {
final List<SessionFactoryServiceInitiator> serviceInitiators = new ArrayList<SessionFactoryServiceInitiator>();
serviceInitiators.add( EventListenerServiceInitiator.INSTANCE );
serviceInitiators.add( StatisticsInitiator.INSTANCE );
return serviceInitiators;
}

View File

@ -0,0 +1,102 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2011, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.stat.internal;
import org.jboss.logging.Logger;
import org.hibernate.HibernateException;
import org.hibernate.HibernateLogger;
import org.hibernate.cfg.Configuration;
import org.hibernate.engine.SessionFactoryImplementor;
import org.hibernate.service.classloading.spi.ClassLoaderService;
import org.hibernate.service.spi.ServiceRegistryImplementor;
import org.hibernate.service.spi.SessionFactoryServiceInitiator;
import org.hibernate.stat.spi.StatisticsFactory;
import org.hibernate.stat.spi.StatisticsImplementor;
/**
* @author Steve Ebersole
*/
public class StatisticsInitiator implements SessionFactoryServiceInitiator<StatisticsImplementor> {
private static final HibernateLogger LOG = Logger.getMessageLogger( HibernateLogger.class, StatisticsInitiator.class.getName() );
public static final StatisticsInitiator INSTANCE = new StatisticsInitiator();
/**
* Names the {@link StatisticsFactory} to use. Recognizes both a class name as well as an instance of
* {@link StatisticsFactory}.
*/
public static final String STATS_BUILDER = "hibernate.stats.factory";
@Override
public Class<StatisticsImplementor> getServiceInitiated() {
return StatisticsImplementor.class;
}
@Override
public StatisticsImplementor initiateService(
SessionFactoryImplementor sessionFactory,
Configuration configuration,
ServiceRegistryImplementor registry) {
final Object configValue = configuration.getProperties().get( STATS_BUILDER );
StatisticsFactory statisticsFactory;
if ( configValue == null ) {
statisticsFactory = DEFAULT_STATS_BUILDER;
}
else if ( StatisticsFactory.class.isInstance( configValue ) ) {
statisticsFactory = (StatisticsFactory) configValue;
}
else {
// assume it names the factory class
final ClassLoaderService classLoaderService = registry.getService( ClassLoaderService.class );
try {
statisticsFactory = (StatisticsFactory) classLoaderService.classForName( configValue.toString() ).newInstance();
}
catch (HibernateException e) {
throw e;
}
catch (Exception e) {
throw new HibernateException(
"Unable to instantiate specified StatisticsFactory implementation [" + configValue.toString() + "]",
e
);
}
}
StatisticsImplementor statistics = statisticsFactory.buildStatistics( sessionFactory );
final boolean enabled = sessionFactory.getSettings().isStatisticsEnabled();
statistics.setStatisticsEnabled( enabled );
LOG.debugf( "Statistics initialized [enabled=%s]", enabled );
return statistics;
}
private static StatisticsFactory DEFAULT_STATS_BUILDER = new StatisticsFactory() {
@Override
public StatisticsImplementor buildStatistics(SessionFactoryImplementor sessionFactory) {
return new ConcurrentStatisticsImpl( sessionFactory );
}
};
}

View File

@ -0,0 +1,34 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2011, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.stat.spi;
import org.hibernate.engine.SessionFactoryImplementor;
import org.hibernate.stat.Statistics;
/**
* @author Steve Ebersole
*/
public interface StatisticsFactory {
public StatisticsImplementor buildStatistics(SessionFactoryImplementor sessionFactory);
}

View File

@ -23,6 +23,7 @@
*/
package org.hibernate.stat.spi;
import org.hibernate.service.Service;
import org.hibernate.stat.Statistics;
/**
@ -31,7 +32,7 @@ import org.hibernate.stat.Statistics;
*
* @author Emmanuel Bernard
*/
public interface StatisticsImplementor extends Statistics {
public interface StatisticsImplementor extends Statistics, Service {
/**
* Callback about a session being opened.
*/