Skip to content

Commit 1223520

Browse files
committed
introduce SessionFactoryLogging
1 parent 84d73d7 commit 1223520

File tree

7 files changed

+188
-90
lines changed

7 files changed

+188
-90
lines changed

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

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import java.io.IOException;
1010
import java.sql.SQLException;
1111
import java.sql.SQLWarning;
12-
import java.util.Map;
1312
import java.util.Properties;
1413
import java.util.ServiceConfigurationError;
1514
import java.util.Set;
@@ -67,22 +66,6 @@ public interface CoreMessageLogger extends BasicLogger {
6766
@Message(value = "Second-level cache disabled", id = 26)
6867
void noRegionFactory();
6968

70-
@LogMessage(level = DEBUG)
71-
@Message(value = "Instantiating factory [%s] with settings: %s", id = 30)
72-
void instantiatingFactory(String uuid, Map<String, Object> settings);
73-
74-
@LogMessage(level = DEBUG)
75-
@Message(value = "Closing factory [%s]", id = 31)
76-
void closingFactory(String uuid);
77-
78-
@LogMessage(level = DEBUG)
79-
@Message(value = "Serializing factory [%s]", id = 32)
80-
void serializingFactory(String uuid);
81-
82-
@LogMessage(level = DEBUG)
83-
@Message(value = "Deserialized factory [%s]", id = 33)
84-
void deserializedFactory(String uuid);
85-
8669
@LogMessage(level = WARN)
8770
@Message(value = "Composite id class does not override equals(): %s", id = 38)
8871
void compositeIdClassDoesNotOverrideEquals(String name);
@@ -306,10 +289,6 @@ void missingArguments(
306289
@Message(value = "Could not close stream on hibernate.properties: %s", id = 297)
307290
void unableToCloseStreamError(IOException error);
308291

309-
@LogMessage(level = ERROR)
310-
@Message(value = "Unable to construct current session context [%s]", id = 302)
311-
void unableToConstructCurrentSessionContext(String impl, @Cause Throwable e);
312-
313292
@LogMessage(level = WARN)
314293
@Message(value = "Unable to close temporary session used to load lazy collection associated to no session", id = 303)
315294
void unableToCloseTemporarySession();

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

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@
127127
import static java.util.Collections.unmodifiableSet;
128128
import static org.hibernate.cfg.AvailableSettings.CURRENT_SESSION_CONTEXT_CLASS;
129129
import static org.hibernate.internal.FetchProfileHelper.addFetchProfiles;
130+
import static org.hibernate.internal.SessionFactoryLogging.SESSION_FACTORY_LOGGER;
130131
import static org.hibernate.internal.SessionFactorySettings.determineJndiName;
131132
import static org.hibernate.internal.SessionFactorySettings.getMaskedSettings;
132133
import static org.hibernate.internal.SessionFactorySettings.getSessionFactoryName;
@@ -155,7 +156,6 @@
155156
* @author Chris Cranford
156157
*/
157158
public class SessionFactoryImpl implements SessionFactoryImplementor {
158-
private static final CoreMessageLogger LOG = CoreLogging.messageLogger( SessionFactoryImpl.class );
159159

160160
private final String name;
161161
private final String jndiName;
@@ -211,7 +211,7 @@ public SessionFactoryImpl(
211211
final MetadataImplementor bootMetamodel,
212212
final SessionFactoryOptions options,
213213
final BootstrapContext bootstrapContext) {
214-
LOG.trace( "Building session factory" );
214+
SESSION_FACTORY_LOGGER.buildingSessionFactory();
215215
typeConfiguration = bootstrapContext.getTypeConfiguration();
216216

217217
sessionFactoryOptions = options;
@@ -228,7 +228,7 @@ public SessionFactoryImpl(
228228
jdbcServices = serviceRegistry.requireService( JdbcServices.class );
229229

230230
settings = getMaskedSettings( options, serviceRegistry );
231-
LOG.instantiatingFactory( uuid, settings );
231+
SESSION_FACTORY_LOGGER.instantiatingFactory( uuid, settings );
232232

233233
sqlStringGenerationContext = createSqlStringGenerationContext( bootMetamodel, options, jdbcServices );
234234

@@ -330,14 +330,12 @@ public SessionFactoryImpl(
330330
close();
331331
}
332332
catch (Exception closeException) {
333-
LOG.trace( "Eating error closing factory after failed instantiation" );
333+
SESSION_FACTORY_LOGGER.eatingErrorClosingFactoryAfterFailedInstantiation();
334334
}
335335
throw e;
336336
}
337337

338-
if ( LOG.isTraceEnabled() ) {
339-
LOG.trace( "Instantiated factory: " + uuid );
340-
}
338+
SESSION_FACTORY_LOGGER.instantiatedFactory( uuid );
341339
}
342340

343341
private JavaType<Object> tenantIdentifierType(SessionFactoryOptions options) {
@@ -762,7 +760,7 @@ public Interceptor getInterceptor() {
762760
@Override
763761
public Reference getReference() {
764762
// from javax.naming.Referenceable
765-
LOG.trace( "Returning a Reference to the factory" );
763+
SESSION_FACTORY_LOGGER.returningReferenceToFactory();
766764
return new Reference(
767765
SessionFactoryImpl.class.getName(),
768766
new StringRefAddr( "uuid", getUuid() ),
@@ -792,16 +790,14 @@ public void close() {
792790
if ( getSessionFactoryOptions().getJpaCompliance().isJpaClosedComplianceEnabled() ) {
793791
throw new IllegalStateException( "EntityManagerFactory is already closed" );
794792
}
795-
796-
LOG.trace( "Already closed" );
793+
SESSION_FACTORY_LOGGER.alreadyClosed();
797794
return;
798795
}
799-
800796
status = Status.CLOSING;
801797
}
802798

803799
try {
804-
LOG.closingFactory( getUuid() );
800+
SESSION_FACTORY_LOGGER.closingFactory( uuid );
805801
observer.sessionFactoryClosing( this );
806802

807803
// NOTE: the null checks below handle cases where close is called
@@ -1036,8 +1032,8 @@ private CurrentSessionContext createSessionContext(String sessionContextType) {
10361032
.getConstructor( new Class[]{ SessionFactoryImplementor.class } )
10371033
.newInstance( this );
10381034
}
1039-
catch ( Throwable t ) {
1040-
LOG.unableToConstructCurrentSessionContext( sessionContextType, t );
1035+
catch ( Throwable throwable ) {
1036+
SESSION_FACTORY_LOGGER.unableToConstructCurrentSessionContext( sessionContextType, throwable );
10411037
return null;
10421038
}
10431039
}
@@ -1109,9 +1105,9 @@ boolean connectionProviderHandlesConnectionSchema() {
11091105
*/
11101106
@Serial
11111107
private void writeObject(ObjectOutputStream out) throws IOException {
1112-
LOG.serializingFactory( getUuid() );
1108+
SESSION_FACTORY_LOGGER.serializingFactory( uuid );
11131109
out.defaultWriteObject();
1114-
LOG.trace( "Serialized factory" );
1110+
SESSION_FACTORY_LOGGER.serializedFactory();
11151111
}
11161112

11171113
/**
@@ -1124,9 +1120,9 @@ private void writeObject(ObjectOutputStream out) throws IOException {
11241120
*/
11251121
@Serial
11261122
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
1127-
LOG.trace( "Deserializing factory" );
1123+
SESSION_FACTORY_LOGGER.deserializingFactory();
11281124
in.defaultReadObject();
1129-
LOG.deserializedFactory( getUuid() );
1125+
SESSION_FACTORY_LOGGER.deserializedFactory( uuid );
11301126
}
11311127

11321128
/**
@@ -1142,27 +1138,27 @@ private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundE
11421138
*/
11431139
@Serial
11441140
private Object readResolve() throws InvalidObjectException {
1145-
LOG.trace( "Resolving serialized factory" );
1146-
return locateSessionFactoryOnDeserialization( getUuid(), name );
1141+
SESSION_FACTORY_LOGGER.trace( "Resolving serialized factory" );
1142+
return locateSessionFactoryOnDeserialization( uuid, name );
11471143
}
11481144

11491145
private static SessionFactory locateSessionFactoryOnDeserialization(String uuid, String name)
11501146
throws InvalidObjectException{
1151-
final SessionFactory uuidResult = SessionFactoryRegistry.INSTANCE.getSessionFactory( uuid );
1147+
final var uuidResult = SessionFactoryRegistry.INSTANCE.getSessionFactory( uuid );
11521148
if ( uuidResult != null ) {
1153-
if ( LOG.isTraceEnabled() ) {
1154-
LOG.trace( "Resolved factory by UUID: " + uuid );
1149+
if ( SESSION_FACTORY_LOGGER.isTraceEnabled() ) {
1150+
SESSION_FACTORY_LOGGER.resolvedFactoryByUuid( uuid );
11551151
}
11561152
return uuidResult;
11571153
}
11581154

11591155
// in case we were deserialized in a different JVM, look for an instance with the same name
11601156
// (provided we were given a name)
11611157
if ( name != null ) {
1162-
final SessionFactory namedResult = SessionFactoryRegistry.INSTANCE.getNamedSessionFactory( name );
1158+
final var namedResult = SessionFactoryRegistry.INSTANCE.getNamedSessionFactory( name );
11631159
if ( namedResult != null ) {
1164-
if ( LOG.isTraceEnabled() ) {
1165-
LOG.trace( "Resolved factory by name: " + name );
1160+
if ( SESSION_FACTORY_LOGGER.isTraceEnabled() ) {
1161+
SESSION_FACTORY_LOGGER.resolvedFactoryByName( name );
11661162
}
11671163
return namedResult;
11681164
}
@@ -1193,7 +1189,7 @@ void serialize(ObjectOutputStream oos) throws IOException {
11931189
* @throws IOException indicates problems reading back serial data stream
11941190
*/
11951191
static SessionFactoryImpl deserialize(ObjectInputStream ois) throws IOException {
1196-
LOG.trace( "Resolving factory from deserialized session" );
1192+
SESSION_FACTORY_LOGGER.resolvingFactoryFromDeserializedSession();
11971193
final String uuid = ois.readUTF();
11981194
boolean isNamed = ois.readBoolean();
11991195
final String name = isNamed ? ois.readUTF() : null;
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.internal;
6+
7+
import org.hibernate.Internal;
8+
import org.hibernate.internal.log.SubSystemLogging;
9+
10+
import org.jboss.logging.BasicLogger;
11+
import org.jboss.logging.Logger;
12+
import org.jboss.logging.annotations.Cause;
13+
import org.jboss.logging.annotations.LogMessage;
14+
import org.jboss.logging.annotations.Message;
15+
import org.jboss.logging.annotations.MessageLogger;
16+
import org.jboss.logging.annotations.ValidIdRange;
17+
18+
import java.lang.invoke.MethodHandles;
19+
import java.util.Map;
20+
21+
import static org.jboss.logging.Logger.Level.DEBUG;
22+
import static org.jboss.logging.Logger.Level.TRACE;
23+
import static org.jboss.logging.Logger.Level.WARN;
24+
25+
/**
26+
* Sub-system logging related to SessionFactory and its registry
27+
*/
28+
@SubSystemLogging(
29+
name = SessionFactoryLogging.NAME,
30+
description = "Logging related to SessionFactory lifecycle, serialization, and registry/JNDI operations"
31+
)
32+
@MessageLogger(projectCode = "HHH")
33+
@ValidIdRange(min = 90006001, max = 90006100)
34+
@Internal
35+
public interface SessionFactoryLogging extends BasicLogger {
36+
String NAME = SubSystemLogging.BASE + ".factory";
37+
38+
SessionFactoryLogging SESSION_FACTORY_LOGGER = Logger.getMessageLogger( MethodHandles.lookup(), SessionFactoryLogging.class, NAME );
39+
40+
// ---- SessionFactoryImpl related ---------------------------------------------------------------
41+
42+
@LogMessage(level = TRACE)
43+
@Message("Building session factory")
44+
void buildingSessionFactory();
45+
46+
@LogMessage(level = DEBUG)
47+
@Message(value = "Instantiating factory [%s] with settings: %s", id = 90006001)
48+
void instantiatingFactory(String uuid, Map<String, Object> settings);
49+
50+
@LogMessage(level = TRACE)
51+
@Message("Eating error closing factory after failed instantiation")
52+
void eatingErrorClosingFactoryAfterFailedInstantiation();
53+
54+
@LogMessage(level = TRACE)
55+
@Message("Instantiated factory: %s")
56+
void instantiatedFactory(String uuid);
57+
58+
@LogMessage(level = TRACE)
59+
@Message("Returning a Reference to the factory")
60+
void returningReferenceToFactory();
61+
62+
@LogMessage(level = TRACE)
63+
@Message("Already closed")
64+
void alreadyClosed();
65+
66+
@LogMessage(level = DEBUG)
67+
@Message(value = "Closing factory [%s]", id = 90006005)
68+
void closingFactory(String uuid);
69+
70+
@LogMessage(level = DEBUG)
71+
@Message(value = "Serializing factory [%s]", id = 90006010)
72+
void serializingFactory(String uuid);
73+
74+
@LogMessage(level = DEBUG)
75+
@Message(value = "Deserialized factory [%s]", id = 90006011)
76+
void deserializedFactory(String uuid);
77+
78+
@LogMessage(level = TRACE)
79+
@Message("Serialized factory")
80+
void serializedFactory();
81+
82+
@LogMessage(level = TRACE)
83+
@Message("Deserializing factory")
84+
void deserializingFactory();
85+
86+
@LogMessage(level = TRACE)
87+
@Message("Resolving serialized factory")
88+
void resolvingSerializedFactory();
89+
90+
@LogMessage(level = TRACE)
91+
@Message("Resolved factory by UUID: %s")
92+
void resolvedFactoryByUuid(String uuid);
93+
94+
@LogMessage(level = TRACE)
95+
@Message("Resolved factory by name: %s")
96+
void resolvedFactoryByName(String name);
97+
98+
@LogMessage(level = TRACE)
99+
@Message("Resolving factory from deserialized session")
100+
void resolvingFactoryFromDeserializedSession();
101+
102+
@LogMessage(level = WARN)
103+
@Message(value = "Unable to construct current session context [%s]", id = 90006030)
104+
void unableToConstructCurrentSessionContext(String sessionContextType, @Cause Throwable throwable);
105+
106+
}

0 commit comments

Comments
 (0)