Skip to content

Commit 84d73d7

Browse files
committed
introduce SessionLogging
1 parent cf3a88a commit 84d73d7

File tree

4 files changed

+205
-65
lines changed

4 files changed

+205
-65
lines changed

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

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
import org.hibernate.HibernateException;
1818
import org.hibernate.Internal;
19-
import org.hibernate.JDBCException;
2019
import org.hibernate.cache.CacheException;
2120
import org.hibernate.id.IntegralDataTypeHolder;
2221
import org.hibernate.type.SerializationException;
@@ -589,12 +588,6 @@ void cannotResolveNonNullableTransientDependencies(
589588
id = 516)
590589
void enhancementDiscoveryFailed(String className, @Cause Throwable cause);
591590

592-
@LogMessage(level = DEBUG)
593-
@Message(value = "JDBCException was thrown for a transaction marked for rollback. " +
594-
" This is probably due to an operation failing fast due to the transaction being marked for rollback.",
595-
id = 520)
596-
void jdbcExceptionThrownWithTransactionRolledBack(@Cause JDBCException e);
597-
598591
@LogMessage(level = DEBUG)
599592
@Message(value = "Flushing and evicting managed instance of type [%s] before removing detached instance with same id",
600593
id = 530)
@@ -614,11 +607,6 @@ void unableToLocateStaticMetamodelField(
614607
String name,
615608
String name2);
616609

617-
@LogMessage(level = DEBUG)
618-
@Message(value = "Returning null (as required by JPA spec) rather than throwing EntityNotFoundException " +
619-
"since the entity of type '%s' with id [%s] does not exist", id = 15013)
620-
void ignoringEntityNotFound(String entityName, String identifier);
621-
622610
@LogMessage(level = DEBUG)
623611
@Message(
624612
id = 15015,

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

Lines changed: 34 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
import static org.hibernate.event.spi.LoadEventListener.INTERNAL_LOAD_LAZY;
108108
import static org.hibernate.event.spi.LoadEventListener.INTERNAL_LOAD_NULLABLE;
109109
import static org.hibernate.internal.LockOptionsHelper.applyPropertiesToLockOptions;
110+
import static org.hibernate.internal.SessionLogging.SESSION_LOGGER;
110111
import static org.hibernate.jpa.HibernateHints.HINT_BATCH_FETCH_SIZE;
111112
import static org.hibernate.jpa.HibernateHints.HINT_ENABLE_SUBSELECT_FETCH;
112113
import static org.hibernate.jpa.HibernateHints.HINT_FETCH_PROFILE;
@@ -154,7 +155,6 @@ public class SessionImpl
154155
extends AbstractSharedSessionContract
155156
implements Serializable, SharedSessionContractImplementor, JdbcSessionOwner, SessionImplementor, EventSource,
156157
TransactionCoordinatorBuilder.Options, WrapperOptions, LoadAccessContext {
157-
private static final CoreMessageLogger LOG = CoreLogging.messageLogger( SessionImpl.class );
158158

159159
// Defaults to null which means the properties are the default
160160
// as defined in FastSessionServices#defaultSessionProperties
@@ -220,9 +220,8 @@ public SessionImpl(SessionFactoryImpl factory, SessionCreationOptions options) {
220220
statistics.openSession();
221221
}
222222

223-
if ( LOG.isTraceEnabled() ) {
224-
LOG.tracef( "Opened Session [%s] at timestamp: %s",
225-
getSessionIdentifier(), currentTimeMillis() );
223+
if ( SESSION_LOGGER.isTraceEnabled() ) {
224+
SESSION_LOGGER.openedSession( getSessionIdentifier(), currentTimeMillis() );
226225
}
227226
}
228227
finally {
@@ -359,16 +358,16 @@ public void close() {
359358
if ( getSessionFactoryOptions().getJpaCompliance().isJpaClosedComplianceEnabled() ) {
360359
throw new IllegalStateException( "EntityManager was already closed" );
361360
}
362-
LOG.trace( "Already closed" );
361+
SESSION_LOGGER.alreadyClosed();
363362
}
364363
else {
365364
closeWithoutOpenChecks();
366365
}
367366
}
368367

369368
public void closeWithoutOpenChecks() {
370-
if ( LOG.isTraceEnabled() ) {
371-
LOG.tracef( "Closing session [%s]", getSessionIdentifier() );
369+
if ( SESSION_LOGGER.isTraceEnabled() ) {
370+
SESSION_LOGGER.closingSession( getSessionIdentifier() );
372371
}
373372

374373
final var eventMonitor = getEventMonitor();
@@ -425,7 +424,7 @@ private boolean isTransactionActiveAndNotMarkedForRollback() {
425424
protected void checkBeforeClosingJdbcCoordinator() {
426425
final var actionQueue = getActionQueue();
427426
if ( actionQueue.hasBeforeTransactionActions() || actionQueue.hasAfterTransactionActions() ) {
428-
LOG.warn( "Closing shared session with unprocessed transaction completion actions" );
427+
SESSION_LOGGER.closingSharedSessionWithUnprocessedTxCompletions();
429428
}
430429
}
431430

@@ -453,17 +452,17 @@ public boolean isOpen() {
453452

454453
protected void checkSessionFactoryOpen() {
455454
if ( !getFactory().isOpen() ) {
456-
LOG.trace( "Forcing-closing session since factory is already closed" );
455+
SESSION_LOGGER.forcingCloseBecauseFactoryClosed();
457456
setClosed();
458457
}
459458
}
460459

461460
private void managedFlush() {
462461
if ( !isOpenOrWaitingForAutoClose() ) {
463-
LOG.trace( "Skipping auto-flush since the session is closed" );
462+
SESSION_LOGGER.skippingAutoFlushSessionClosed();
464463
}
465464
else {
466-
LOG.trace( "Automatically flushing session" );
465+
SESSION_LOGGER.automaticallyFlushingSession();
467466
fireFlush();
468467
}
469468
}
@@ -484,7 +483,7 @@ else if ( isClosed() ) {
484483
}
485484

486485
private void managedClose() {
487-
LOG.trace( "Automatically closing session" );
486+
SESSION_LOGGER.automaticallyClosingSession();
488487
closeWithoutOpenChecks();
489488
}
490489

@@ -825,7 +824,7 @@ private void fireMerge(final MergeContext mergeContext, final MergeEvent mergeEv
825824
public void delete(String entityName, Object object, boolean isCascadeDeleteEnabled, DeleteContext transientEntities) {
826825
checkOpenOrWaitingForAutoClose();
827826
final boolean removingOrphanBeforeUpdates = persistenceContext.isRemovingOrphanBeforeUpdates();
828-
final boolean traceEnabled = LOG.isTraceEnabled();
827+
final boolean traceEnabled = SESSION_LOGGER.isTraceEnabled();
829828
if ( traceEnabled && removingOrphanBeforeUpdates ) {
830829
logRemoveOrphanBeforeUpdates( "before continuing", entityName, object );
831830
}
@@ -842,7 +841,7 @@ public void delete(String entityName, Object object, boolean isCascadeDeleteEnab
842841
public void removeOrphanBeforeUpdates(String entityName, Object child) {
843842
// TODO: The removeOrphan concept is a temporary "hack" for HHH-6484.
844843
// This should be removed once action/task ordering is improved.
845-
final boolean traceEnabled = LOG.isTraceEnabled();
844+
final boolean traceEnabled = SESSION_LOGGER.isTraceEnabled();
846845
if ( traceEnabled ) {
847846
logRemoveOrphanBeforeUpdates( "begin", entityName, child );
848847
}
@@ -860,13 +859,10 @@ public void removeOrphanBeforeUpdates(String entityName, Object child) {
860859
}
861860

862861
private void logRemoveOrphanBeforeUpdates(String timing, String entityName, Object entity) {
863-
if ( LOG.isTraceEnabled() ) {
862+
if ( SESSION_LOGGER.isTraceEnabled() ) {
864863
final var entityEntry = persistenceContext.getEntry( entity );
865-
LOG.tracef(
866-
"%s remove orphan before updates: [%s]",
867-
timing,
868-
entityEntry == null ? entityName : infoString( entityName, entityEntry.getId() )
869-
);
864+
final String entityInfo = entityEntry == null ? entityName : infoString( entityName, entityEntry.getId() );
865+
SESSION_LOGGER.removeOrphanBeforeUpdates( timing, entityInfo );
870866
}
871867
}
872868

@@ -1002,9 +998,9 @@ public Object get(String entityName, Object id) {
1002998
*/
1003999
@Override
10041000
public Object immediateLoad(String entityName, Object id) {
1005-
if ( LOG.isDebugEnabled() ) {
1001+
if ( SESSION_LOGGER.isDebugEnabled() ) {
10061002
final var persister = requireEntityPersister( entityName );
1007-
LOG.tracef( "Initializing proxy: %s", infoString( persister, id, getFactory() ) );
1003+
SESSION_LOGGER.initializingProxy( infoString( persister, id, getFactory() ) );
10081004
}
10091005
final var event = makeLoadEvent( entityName, id, getReadOnlyFromLoadQueryInfluencers(), true );
10101006
fireLoadNoChecks( event, IMMEDIATE_LOAD );
@@ -1026,7 +1022,7 @@ public Object internalLoad(String entityName, Object id, boolean eager, boolean
10261022
clearedEffectiveGraph = false;
10271023
}
10281024
else {
1029-
LOG.trace( "Clearing effective entity graph for subsequent select" );
1025+
SESSION_LOGGER.clearingEffectiveEntityGraph();
10301026
clearedEffectiveGraph = true;
10311027
effectiveEntityGraph.clear();
10321028
}
@@ -1467,9 +1463,8 @@ public void forceFlush(EntityEntry entityEntry) {
14671463

14681464
@Override
14691465
public void forceFlush(EntityKey key) {
1470-
if ( LOG.isTraceEnabled() ) {
1471-
LOG.tracef("Flushing to force deletion of re-saved object: "
1472-
+ infoString( key.getPersister(), key.getIdentifier(), getFactory() ) );
1466+
if ( SESSION_LOGGER.isTraceEnabled() ) {
1467+
SESSION_LOGGER.flushingToForceDeletion( infoString( key.getPersister(), key.getIdentifier(), getFactory() ) );
14731468
}
14741469

14751470
if ( persistenceContext.getCascadeLevel() > 0 ) {
@@ -1839,7 +1834,7 @@ public String toString() {
18391834
.append( "SessionImpl(" )
18401835
.append( System.identityHashCode( this ) );
18411836
if ( !isClosed() ) {
1842-
if ( LOG.isTraceEnabled() ) {
1837+
if ( SESSION_LOGGER.isTraceEnabled() ) {
18431838
string.append( persistenceContext )
18441839
.append( ";" )
18451840
.append( actionQueue );
@@ -1991,7 +1986,7 @@ public LobHelper getLobHelper() {
19911986

19921987
@Override
19931988
public void beforeTransactionCompletion() {
1994-
LOG.trace( "SessionImpl#beforeTransactionCompletion()" );
1989+
SESSION_LOGGER.beforeTransactionCompletion();
19951990
flushBeforeTransactionCompletion();
19961991
actionQueue.beforeTransactionCompletion();
19971992
beforeTransactionCompletionEvents();
@@ -2000,8 +1995,8 @@ public void beforeTransactionCompletion() {
20001995

20011996
@Override
20021997
public void afterTransactionCompletion(boolean successful, boolean delayed) {
2003-
if ( LOG.isTraceEnabled() ) {
2004-
LOG.tracef( "SessionImpl#afterTransactionCompletion(successful=%s, delayed=%s)", successful, delayed );
1998+
if ( SESSION_LOGGER.isTraceEnabled() ) {
1999+
SESSION_LOGGER.afterTransactionCompletion( successful, delayed );
20052000
}
20062001

20072002
final boolean notClosed = isOpenOrWaitingForAutoClose();
@@ -2193,7 +2188,7 @@ private <T> T find(Class<T> entityClass, Object primaryKey, LockOptions lockOpti
21932188
if ( accessTransaction().isActive() && accessTransaction().getRollbackOnly() ) {
21942189
// Assume situation HHH-12472 running on WildFly
21952190
// Just log the exception and return null
2196-
LOG.jdbcExceptionThrownWithTransactionRolledBack( e );
2191+
SESSION_LOGGER.jdbcExceptionThrownWithTransactionRolledBack( e );
21972192
return null;
21982193
}
21992194
else {
@@ -2211,8 +2206,8 @@ private <T> T find(Class<T> entityClass, Object primaryKey, LockOptions lockOpti
22112206

22122207
// Hibernate Reactive calls this
22132208
protected static <T> void logIgnoringEntityNotFound(Class<T> entityClass, Object primaryKey) {
2214-
if ( LOG.isDebugEnabled() ) {
2215-
LOG.ignoringEntityNotFound(
2209+
if ( SESSION_LOGGER.isDebugEnabled() ) {
2210+
SESSION_LOGGER.ignoringEntityNotFound(
22162211
entityClass != null ? entityClass.getName(): null,
22172212
primaryKey != null ? primaryKey.toString() : null
22182213
);
@@ -2544,12 +2539,12 @@ public void setProperty(String propertyName, Object value) {
25442539
checkOpen();
25452540

25462541
if ( !( value instanceof Serializable ) ) {
2547-
LOG.warnf( "Property '%s' is not serializable, value won't be set", propertyName );
2542+
SESSION_LOGGER.nonSerializableProperty( propertyName );
25482543
return;
25492544
}
25502545

25512546
if ( propertyName == null ) {
2552-
LOG.warn( "Property having key null is illegal, value won't be set" );
2547+
SESSION_LOGGER.nullPropertyKey();
25532548
return;
25542549
}
25552550

@@ -2767,8 +2762,8 @@ public <E> Collection<E> getManagedEntities(EntityType<E> entityType) {
27672762
*/
27682763
@Serial
27692764
private void writeObject(ObjectOutputStream oos) throws IOException {
2770-
if ( LOG.isTraceEnabled() ) {
2771-
LOG.tracef( "Serializing Session [%s]", getSessionIdentifier() );
2765+
if ( SESSION_LOGGER.isTraceEnabled() ) {
2766+
SESSION_LOGGER.serializingSession( getSessionIdentifier() );
27722767
}
27732768

27742769
oos.defaultWriteObject();
@@ -2789,8 +2784,8 @@ private void writeObject(ObjectOutputStream oos) throws IOException {
27892784
*/
27902785
@Serial
27912786
private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException, SQLException {
2792-
if ( LOG.isTraceEnabled() ) {
2793-
LOG.tracef( "Deserializing Session [%s]", getSessionIdentifier() );
2787+
if ( SESSION_LOGGER.isTraceEnabled() ) {
2788+
SESSION_LOGGER.deserializingSession( getSessionIdentifier() );
27942789
}
27952790

27962791
ois.defaultReadObject();

0 commit comments

Comments
 (0)