Skip to content

Commit c9fa8aa

Browse files
committed
HHH-18907 fix weird way of enabling logging of session metric
hibernate.session.events.log is now obsolete also deprecate obsolete BaseSessionEventListener class
1 parent a1b4773 commit c9fa8aa

File tree

12 files changed

+142
-204
lines changed

12 files changed

+142
-204
lines changed

hibernate-core/src/main/java/org/hibernate/BaseSessionEventListener.java

Lines changed: 6 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -5,97 +5,14 @@
55
package org.hibernate;
66

77
/**
8-
* A no-op implementation of SessionEventListener. Intended as a convenient base class for developing
9-
* SessionEventListener implementations.
8+
* A noop implementation of {@link SessionEventListener}.
9+
* Intended as a convenient base class for developing
10+
* {@code SessionEventListener} implementations.
1011
*
1112
* @author Steve Ebersole
13+
*
14+
* @deprecated Just implement {@link SessionEventListener} directly.
1215
*/
16+
@Deprecated(since = "7", forRemoval = true)
1317
public class BaseSessionEventListener implements SessionEventListener {
14-
@Override
15-
public void transactionCompletion(boolean successful) {
16-
}
17-
18-
@Override
19-
public void jdbcConnectionAcquisitionStart() {
20-
}
21-
22-
@Override
23-
public void jdbcConnectionAcquisitionEnd() {
24-
}
25-
26-
@Override
27-
public void jdbcConnectionReleaseStart() {
28-
}
29-
30-
@Override
31-
public void jdbcConnectionReleaseEnd() {
32-
}
33-
34-
@Override
35-
public void jdbcPrepareStatementStart() {
36-
}
37-
38-
@Override
39-
public void jdbcPrepareStatementEnd() {
40-
}
41-
42-
@Override
43-
public void jdbcExecuteStatementStart() {
44-
}
45-
46-
@Override
47-
public void jdbcExecuteStatementEnd() {
48-
}
49-
50-
@Override
51-
public void jdbcExecuteBatchStart() {
52-
}
53-
54-
@Override
55-
public void jdbcExecuteBatchEnd() {
56-
}
57-
58-
@Override
59-
public void cachePutStart() {
60-
}
61-
62-
@Override
63-
public void cachePutEnd() {
64-
}
65-
66-
@Override
67-
public void cacheGetStart() {
68-
}
69-
70-
@Override
71-
public void cacheGetEnd(boolean hit) {
72-
}
73-
74-
@Override
75-
public void flushStart() {
76-
}
77-
78-
@Override
79-
public void flushEnd(int numberOfEntities, int numberOfCollections) {
80-
}
81-
82-
@Override
83-
public void partialFlushStart() {
84-
}
85-
86-
@Override
87-
public void partialFlushEnd(int numberOfEntities, int numberOfCollections) {
88-
}
89-
90-
@Override
91-
public void dirtyCalculationStart() {
92-
}
93-
94-
@Override
95-
public void dirtyCalculationEnd(boolean dirty) {
96-
}
97-
98-
@Override
99-
public void end() {
100-
}
10118
}

hibernate-core/src/main/java/org/hibernate/SessionEventListener.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
* sessions may be registered using the configuration property
1616
* {@value org.hibernate.cfg.AvailableSettings#AUTO_SESSION_EVENTS_LISTENER}.
1717
* A new instance of the class will be created for each new session.
18-
* <p>
19-
* <em>This an incubating API, subject to change.</em>
18+
*
19+
* @apiNote This an incubating API, subject to change.
2020
*
2121
* @see org.hibernate.cfg.AvailableSettings#AUTO_SESSION_EVENTS_LISTENER
2222
* @see SessionBuilder#eventListeners(SessionEventListener...)

hibernate-core/src/main/java/org/hibernate/boot/internal/SessionFactoryOptionsBuilder.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@
100100
import static org.hibernate.cfg.AvailableSettings.JDBC_TIME_ZONE;
101101
import static org.hibernate.cfg.AvailableSettings.JPA_CALLBACKS_ENABLED;
102102
import static org.hibernate.cfg.AvailableSettings.JTA_TRACK_BY_THREAD;
103-
import static org.hibernate.cfg.AvailableSettings.LOG_SESSION_METRICS;
104103
import static org.hibernate.cfg.AvailableSettings.MAX_FETCH_DEPTH;
105104
import static org.hibernate.cfg.AvailableSettings.MULTI_TENANT_IDENTIFIER_RESOLVER;
106105
import static org.hibernate.cfg.AvailableSettings.ORDER_INSERTS;
@@ -357,8 +356,7 @@ public SessionFactoryOptionsBuilder(StandardServiceRegistry serviceRegistry, Boo
357356
? null
358357
: strategySelector.selectStrategyImplementor( SessionEventListener.class, autoSessionEventsListenerName );
359358

360-
final boolean logSessionMetrics = configurationService.getSetting( LOG_SESSION_METRICS, BOOLEAN, statisticsEnabled );
361-
this.baselineSessionEventsListenerBuilder = new BaselineSessionEventsListenerBuilder( logSessionMetrics, autoSessionEventsListener );
359+
this.baselineSessionEventsListenerBuilder = new BaselineSessionEventsListenerBuilder( autoSessionEventsListener );
362360

363361
this.customEntityDirtinessStrategy = strategySelector.resolveDefaultableStrategy(
364362
CustomEntityDirtinessStrategy.class,

hibernate-core/src/main/java/org/hibernate/cache/spi/SecondLevelCacheLogger.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ public interface SecondLevelCacheLogger extends BasicLogger {
8484
" You can disable this warning by setting '%2$s' to '%3$s'.",
8585
id = NAMESPACE + 6
8686
)
87+
@SuppressWarnings( "unused" ) // used by hibernate-jcache
8788
void missingCacheCreated(String regionName, String configurationPropertyToDisableKey, String configurationPropertyToDisableValue);
8889

8990
@LogMessage(level = WARN)
@@ -92,6 +93,7 @@ public interface SecondLevelCacheLogger extends BasicLogger {
9293
" Update your configuration to rename cache [%2$s] to [%1$s].",
9394
id = NAMESPACE + 7
9495
)
96+
@SuppressWarnings( "unused" ) // used by hibernate-jcache
9597
void usingLegacyCacheName(String currentName, String legacyName);
9698

9799
@LogMessage(level = WARN)
@@ -100,6 +102,7 @@ public interface SecondLevelCacheLogger extends BasicLogger {
100102
" Make sure your cache implementation supports JTA transactions.",
101103
id = NAMESPACE + 8
102104
)
105+
@SuppressWarnings( "unused" ) // used by hibernate-jcache
103106
void nonStandardSupportForAccessType(String key, String accessType, String regionName);
104107

105108
}

hibernate-core/src/main/java/org/hibernate/cfg/PersistenceSettings.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,6 @@ public interface PersistenceSettings {
134134
*
135135
* @see #SCANNER
136136
* @see org.hibernate.boot.archive.scan.spi.Scanner
137-
* @see org.hibernate.boot.archive.scan.spi.AbstractScannerImpl
138137
* @see org.hibernate.boot.MetadataBuilder#applyArchiveDescriptorFactory
139138
*/
140139
String SCANNER_ARCHIVE_INTERPRETER = "hibernate.archive.interpreter";

hibernate-core/src/main/java/org/hibernate/cfg/SessionEventSettings.java

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,6 @@
1313
*/
1414
public interface SessionEventSettings {
1515

16-
/**
17-
* Controls whether {@linkplain org.hibernate.stat.SessionStatistics session metrics}
18-
* should be {@linkplain org.hibernate.engine.internal.StatisticalLoggingSessionEventListener
19-
* logged} for any session in which statistics are being collected.
20-
* <p>
21-
* By default, logging of session metrics is disabled unless {@link StatisticsSettings#GENERATE_STATISTICS}
22-
* is enabled.
23-
*
24-
* @settingDefault Defined by {@link StatisticsSettings#GENERATE_STATISTICS}
25-
*/
26-
String LOG_SESSION_METRICS = "hibernate.session.events.log";
27-
2816
/**
2917
* Defines a default {@link org.hibernate.SessionEventListener} to be applied to
3018
* newly-opened {@link org.hibernate.Session}s.
@@ -75,4 +63,12 @@ public interface SessionEventSettings {
7563
* @since 5.2
7664
*/
7765
String SESSION_SCOPED_INTERCEPTOR = "hibernate.session_factory.session_scoped_interceptor";
66+
67+
/**
68+
* @deprecated This setting is now ignored. Enable the log category
69+
* {@code org.hibernate.session.metrics} to automatically
70+
* collect and log session-level metrics.
71+
*/
72+
@Deprecated(since = "7", forRemoval = true)
73+
String LOG_SESSION_METRICS = "hibernate.session.events.log";
7874
}

hibernate-core/src/main/java/org/hibernate/engine/internal/StatisticalLoggingSessionEventListener.java

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,26 @@
44
*/
55
package org.hibernate.engine.internal;
66

7-
import org.hibernate.BaseSessionEventListener;
7+
import org.hibernate.SessionEventListener;
8+
import org.hibernate.internal.CoreMessageLogger;
89

9-
import org.jboss.logging.Logger;
10+
import static org.hibernate.internal.CoreLogging.messageLogger;
1011

1112
/**
12-
* @see org.hibernate.cfg.AvailableSettings#LOG_SESSION_METRICS
13+
* Tracks and logs certain session-level metrics.
1314
*
1415
* @author Steve Ebersole
1516
*/
16-
public class StatisticalLoggingSessionEventListener extends BaseSessionEventListener {
17-
private static final Logger log = Logger.getLogger( StatisticalLoggingSessionEventListener.class );
17+
public class StatisticalLoggingSessionEventListener implements SessionEventListener {
18+
private static final CoreMessageLogger log = messageLogger( "org.hibernate.session.metrics" );
1819

1920
/**
2021
* Used by SettingsFactory (in conjunction with stats being enabled) to determine whether to apply this listener
2122
*
2223
* @return {@code true} if logging is enabled for this listener.
2324
*/
2425
public static boolean isLoggingEnabled() {
25-
return log.isInfoEnabled();
26+
return log.isDebugEnabled();
2627
}
2728

2829
// cumulative state ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -274,21 +275,8 @@ public void partialFlushEnd(int numberOfEntities, int numberOfCollections) {
274275

275276
@Override
276277
public void end() {
277-
if ( log.isInfoEnabled() ) {
278-
log.infof(
279-
"Session Metrics {\n" +
280-
" %s nanoseconds spent acquiring %s JDBC connections;\n" +
281-
" %s nanoseconds spent releasing %s JDBC connections;\n" +
282-
" %s nanoseconds spent preparing %s JDBC statements;\n" +
283-
" %s nanoseconds spent executing %s JDBC statements;\n" +
284-
" %s nanoseconds spent executing %s JDBC batches;\n" +
285-
" %s nanoseconds spent performing %s L2C puts;\n" +
286-
" %s nanoseconds spent performing %s L2C hits;\n" +
287-
" %s nanoseconds spent performing %s L2C misses;\n" +
288-
" %s nanoseconds spent executing %s flushes (flushing a total of %s entities and %s collections);\n" +
289-
" %s nanoseconds spent executing %s pre-partial-flushes;\n" +
290-
" %s nanoseconds spent executing %s partial-flushes (flushing a total of %s entities and %s collections)\n" +
291-
"}",
278+
if ( isLoggingEnabled() ) {
279+
log.sessionMetrics(
292280
jdbcConnectionAcquisitionTime,
293281
jdbcConnectionAcquisitionCount,
294282
jdbcConnectionReleaseTime,

hibernate-core/src/main/java/org/hibernate/internal/BaselineSessionEventsListenerBuilder.java

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,12 @@ public class BaselineSessionEventsListenerBuilder {
1919

2020
private static final SessionEventListener[] EMPTY = new SessionEventListener[0];
2121

22-
private final boolean logSessionMetrics;
2322
private final Class<? extends SessionEventListener> autoListener;
2423

25-
public BaselineSessionEventsListenerBuilder(
26-
boolean logSessionMetrics,
27-
Class<? extends SessionEventListener> autoListener) {
28-
this.logSessionMetrics = logSessionMetrics;
24+
public BaselineSessionEventsListenerBuilder(Class<? extends SessionEventListener> autoListener) {
2925
this.autoListener = autoListener;
3026
}
3127

32-
@SuppressWarnings("unused")
33-
public boolean isLogSessionMetrics() {
34-
return logSessionMetrics;
35-
}
36-
3728
@SuppressWarnings("unused")
3829
public Class<? extends SessionEventListener> getAutoListener() {
3930
return autoListener;
@@ -48,24 +39,26 @@ public List<SessionEventListener> buildBaselineList() {
4839
}
4940

5041
public SessionEventListener[] buildBaseline() {
51-
final boolean addStats = logSessionMetrics && StatisticalLoggingSessionEventListener.isLoggingEnabled();
52-
final boolean addAutoListener = autoListener != null;
5342
final SessionEventListener[] arr;
54-
if ( addStats && addAutoListener ) {
55-
arr = new SessionEventListener[2];
56-
arr[0] = buildStatsListener();
57-
arr[1] = buildAutoListener( autoListener );
58-
}
59-
else if ( !addStats && addAutoListener ) {
60-
arr = new SessionEventListener[1];
61-
arr[0] = buildAutoListener( autoListener );
62-
}
63-
else if ( addStats && !addAutoListener ) {
64-
arr = new SessionEventListener[1];
65-
arr[0] = buildStatsListener();
43+
if ( autoListener != null ) {
44+
if ( StatisticalLoggingSessionEventListener.isLoggingEnabled() ) {
45+
arr = new SessionEventListener[2];
46+
arr[0] = buildStatsListener();
47+
arr[1] = buildAutoListener( autoListener );
48+
}
49+
else {
50+
arr = new SessionEventListener[1];
51+
arr[0] = buildAutoListener( autoListener );
52+
}
6653
}
67-
else {//if ( !addStats && !addAutoListener )
68-
arr = EMPTY;
54+
else {
55+
if ( StatisticalLoggingSessionEventListener.isLoggingEnabled() ) {
56+
arr = new SessionEventListener[1];
57+
arr[0] = buildStatsListener();
58+
}
59+
else {
60+
arr = EMPTY;
61+
}
6962
}
7063
return arr;
7164
}
@@ -76,7 +69,7 @@ private static SessionEventListener buildAutoListener(final Class<? extends Sess
7669
}
7770
catch (Exception e) {
7871
throw new HibernateException(
79-
"Unable to instantiate specified auto SessionEventListener : " + autoListener.getName(),
72+
"Unable to instantiate specified auto SessionEventListener: " + autoListener.getName(),
8073
e
8174
);
8275
}

hibernate-core/src/main/java/org/hibernate/internal/CoreMessageLogger.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -887,4 +887,50 @@ void unableToLocateStaticMetamodelField(
887887
value = "Encountered multiple persistence-unit stanzas defining same name [%s]; persistence-unit names must be unique"
888888
)
889889
void duplicatedPersistenceUnitName(String name);
890+
891+
@LogMessage(level = DEBUG)
892+
@Message(
893+
id = 425,
894+
value = """
895+
Logging session metrics:
896+
%s ns acquiring %s JDBC connections
897+
%s ns releasing %s JDBC connections
898+
%s ns preparing %s JDBC statements
899+
%s ns executing %s JDBC statements
900+
%s ns executing %s JDBC batches
901+
%s ns performing %s second-level cache puts
902+
%s ns performing %s second-level cache hits
903+
%s ns performing %s second-level cache misses
904+
%s ns executing %s flushes (flushing a total of %s entities and %s collections)
905+
%s ns executing %s pre-partial-flushes
906+
%s ns executing %s partial-flushes (flushing a total of %s entities and %s collections)
907+
"""
908+
)
909+
void sessionMetrics(
910+
long jdbcConnectionAcquisitionTime,
911+
int jdbcConnectionAcquisitionCount,
912+
long jdbcConnectionReleaseTime,
913+
int jdbcConnectionReleaseCount,
914+
long jdbcPrepareStatementTime,
915+
int jdbcPrepareStatementCount,
916+
long jdbcExecuteStatementTime,
917+
int jdbcExecuteStatementCount,
918+
long jdbcExecuteBatchTime,
919+
int jdbcExecuteBatchCount,
920+
long cachePutTime,
921+
int cachePutCount,
922+
long cacheHitTime,
923+
int cacheHitCount,
924+
long cacheMissTime,
925+
int cacheMissCount,
926+
long flushTime,
927+
int flushCount,
928+
long flushEntityCount,
929+
long flushCollectionCount,
930+
long prePartialFlushTime,
931+
int prePartialFlushCount,
932+
long partialFlushTime,
933+
int partialFlushCount,
934+
long partialFlushEntityCount,
935+
long partialFlushCollectionCount);
890936
}

0 commit comments

Comments
 (0)