improve documentation of stats.spi

This commit is contained in:
Gavin King 2024-12-12 10:32:57 +01:00
parent 7780b74c21
commit fc58d614d6
4 changed files with 40 additions and 29 deletions

View File

@ -10,13 +10,21 @@
import org.checkerframework.checker.nullness.qual.Nullable; import org.checkerframework.checker.nullness.qual.Nullable;
/** /**
* Exposes statistics collected from all sessions belonging to a * Exposes statistics collected from all sessions belonging to a given
* particular {@link org.hibernate.SessionFactory}. * {@link org.hibernate.SessionFactory}.
* <ul>
* <li>Collection of statistics is enabled if the configuration property
* {@value org.hibernate.cfg.AvailableSettings#GENERATE_STATISTICS}
* is set to {@code true}.
* <li>Alternatively, statistics collection may be enabled or disabled
* at runtime by calling {@link #setStatisticsEnabled(boolean)}.
* </ul>
* <p> * <p>
* Collection of statistics is enabled if the configuration property * A custom statistics collector may be supplied by implementing the
* {@value org.hibernate.cfg.AvailableSettings#GENERATE_STATISTICS} is * {@link org.hibernate.stat.spi.StatisticsImplementor} SPI, and
* set to {@code true}. It may be dynamically enabled or disabled at * supplying a {@link org.hibernate.stat.spi.StatisticsFactory} via
* runtime by calling {@link #setStatisticsEnabled(boolean)}. * the configuration setting
* {@value org.hibernate.cfg.StatisticsSettings#STATS_BUILDER}.
* *
* @author Emmanuel Bernard * @author Emmanuel Bernard
*/ */

View File

@ -38,10 +38,9 @@ public Class<StatisticsImplementor> getServiceInitiated() {
@Override @Override
public StatisticsImplementor initiateService(SessionFactoryServiceInitiatorContext context) { public StatisticsImplementor initiateService(SessionFactoryServiceInitiatorContext context) {
final Object configValue = context.getServiceRegistry() final Object configValue =
.requireService( ConfigurationService.class ) context.getServiceRegistry().requireService( ConfigurationService.class )
.getSettings() .getSettings().get( STATS_BUILDER );
.get( STATS_BUILDER );
return initiateServiceInternal( context.getSessionFactory(), configValue, context.getServiceRegistry() ); return initiateServiceInternal( context.getSessionFactory(), configValue, context.getServiceRegistry() );
} }
@ -50,18 +49,30 @@ private StatisticsImplementor initiateServiceInternal(
@Nullable Object configValue, @Nullable Object configValue,
ServiceRegistryImplementor registry) { ServiceRegistryImplementor registry) {
final StatisticsFactory statisticsFactory; final StatisticsFactory statisticsFactory = statisticsFactory( configValue, registry );
final StatisticsImplementor statistics =
statisticsFactory == null
? new StatisticsImpl( sessionFactory ) // default impl
: statisticsFactory.buildStatistics( sessionFactory );
final boolean enabled = sessionFactory.getSessionFactoryOptions().isStatisticsEnabled();
statistics.setStatisticsEnabled( enabled );
LOG.debugf( "Statistics initialized [enabled=%s]", enabled );
return statistics;
}
private static @Nullable StatisticsFactory statisticsFactory(
@Nullable Object configValue, ServiceRegistryImplementor registry) {
if ( configValue == null ) { if ( configValue == null ) {
statisticsFactory = null; //We'll use the default return null; //We'll use the default
} }
else if ( configValue instanceof StatisticsFactory ) { else if ( configValue instanceof StatisticsFactory factory ) {
statisticsFactory = (StatisticsFactory) configValue; return factory;
} }
else { else {
// assume it names the factory class // assume it names the factory class
final ClassLoaderService classLoaderService = registry.requireService( ClassLoaderService.class ); final ClassLoaderService classLoaderService = registry.requireService( ClassLoaderService.class );
try { try {
statisticsFactory = (StatisticsFactory) classLoaderService.classForName( configValue.toString() ).newInstance(); return (StatisticsFactory) classLoaderService.classForName( configValue.toString() ).newInstance();
} }
catch (HibernateException e) { catch (HibernateException e) {
throw e; throw e;
@ -73,18 +84,5 @@ else if ( configValue instanceof StatisticsFactory ) {
); );
} }
} }
final StatisticsImplementor statistics;
if ( statisticsFactory == null ) {
// Default:
statistics = new StatisticsImpl( sessionFactory );
}
else {
statistics = statisticsFactory.buildStatistics( sessionFactory );
}
final boolean enabled = sessionFactory.getSessionFactoryOptions().isStatisticsEnabled();
statistics.setStatisticsEnabled( enabled );
LOG.debugf( "Statistics initialized [enabled=%s]", enabled );
return statistics;
} }
} }

View File

@ -8,6 +8,9 @@
/** /**
* Factory for custom implementations of {@link StatisticsImplementor}. * Factory for custom implementations of {@link StatisticsImplementor}.
* <p>
* A custom implementation may be selected via the configuration property
* {@value org.hibernate.cfg.StatisticsSettings#STATS_BUILDER}.
* *
* @author Steve Ebersole * @author Steve Ebersole
*/ */

View File

@ -13,7 +13,9 @@
import static java.util.Collections.emptyMap; import static java.util.Collections.emptyMap;
/** /**
* A service SPI for collecting statistics about various events that occur at runtime. * A service SPI for collecting statistics about various events occurring at runtime.
* <p>
* A custom implementation may be provided via a {@link StatisticsFactory}.
* *
* @author Emmanuel Bernard * @author Emmanuel Bernard
*/ */