Skip to content

Commit a6601ba

Browse files
committed
introduce CollectionLogger
1 parent b77bd56 commit a6601ba

File tree

7 files changed

+165
-144
lines changed

7 files changed

+165
-144
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.collection.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.LogMessage;
13+
import org.jboss.logging.annotations.Message;
14+
import org.jboss.logging.annotations.MessageLogger;
15+
import org.jboss.logging.annotations.ValidIdRange;
16+
17+
import java.lang.invoke.MethodHandles;
18+
19+
import static org.jboss.logging.Logger.Level.DEBUG;
20+
import static org.jboss.logging.Logger.Level.INFO;
21+
import static org.jboss.logging.Logger.Level.WARN;
22+
23+
/**
24+
* Subsystem logging related to PersistentCollection runtime events
25+
*/
26+
@SubSystemLogging(
27+
name = CollectionLogger.NAME,
28+
description = "Logging related to persistent collection lifecycle and operations"
29+
)
30+
@MessageLogger(projectCode = "HHH")
31+
@ValidIdRange(min = 90030001, max = 90031000)
32+
@Internal
33+
public interface CollectionLogger extends BasicLogger {
34+
String NAME = SubSystemLogging.BASE + ".collection";
35+
36+
CollectionLogger COLLECTION_LOGGER = Logger.getMessageLogger( MethodHandles.lookup(), CollectionLogger.class, NAME );
37+
38+
@LogMessage(level = WARN)
39+
@Message(id = 90030001, value = "Unable to close temporary session used to load lazy collection associated to no session")
40+
void unableToCloseTemporarySession();
41+
42+
@LogMessage(level = WARN)
43+
@Message(id = 90030002, value = "Detaching an uninitialized collection with enabled filters from a session: %s")
44+
void enabledFiltersWhenDetachFromSession(String collectionInfoString);
45+
46+
@LogMessage(level = WARN)
47+
@Message(id = 90030004, value = "Attaching an uninitialized collection with queued operations to a session: %s")
48+
void queuedOperationWhenAttachToSession(String collectionInfoString);
49+
50+
@LogMessage(level = INFO)
51+
@Message(id = 90030005, value = "Detaching an uninitialized collection with queued operations from a session: %s")
52+
void queuedOperationWhenDetachFromSession(String collectionInfoString);
53+
54+
@LogMessage(level = DEBUG)
55+
@Message(id = 90030006, value = "Detaching an uninitialized collection with queued operations from a session due to rollback: %s")
56+
void queuedOperationWhenDetachFromSessionOnRollback(String collectionInfoString);
57+
58+
@LogMessage(level = WARN)
59+
@Message(id = 90030007, value = "Cannot unset session in a collection because an unexpected session is defined."
60+
+ " A persistent collection may only be associated with one session at a time. %s")
61+
void logCannotUnsetUnexpectedSessionInCollection(String msg);
62+
63+
@LogMessage(level = WARN)
64+
@Message(id = 90030008, value = "An unexpected session is defined for a collection, but the collection is not connected to that session."
65+
+ " A persistent collection may only be associated with one session at a time. Overwriting session. %s")
66+
void logUnexpectedSessionInCollectionNotConnected(String msg);
67+
}

hibernate-core/src/main/java/org/hibernate/collection/spi/AbstractPersistentCollection.java

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@
2727
import org.hibernate.engine.spi.SharedSessionContractImplementor;
2828
import org.hibernate.engine.spi.Status;
2929
import org.hibernate.engine.spi.TypedValue;
30-
import org.hibernate.internal.CoreLogging;
31-
import org.hibernate.internal.CoreMessageLogger;
3230
import org.hibernate.internal.SessionFactoryRegistry;
3331
import org.hibernate.internal.util.MarkerObject;
3432
import org.hibernate.internal.util.collections.IdentitySet;
@@ -40,6 +38,7 @@
4038
import org.checkerframework.checker.nullness.qual.Nullable;
4139

4240
import static java.util.Collections.emptyList;
41+
import static org.hibernate.collection.internal.CollectionLogger.COLLECTION_LOGGER;
4342
import static org.hibernate.engine.internal.ForeignKeys.getEntityIdentifier;
4443
import static org.hibernate.engine.internal.ForeignKeys.getEntityIdentifierIfNotUnsaved;
4544
import static org.hibernate.engine.internal.ForeignKeys.isNotTransient;
@@ -57,8 +56,6 @@
5756
*/
5857
public abstract class AbstractPersistentCollection<E> implements Serializable, PersistentCollection<E> {
5958

60-
private static final CoreMessageLogger LOG = CoreLogging.messageLogger( AbstractPersistentCollection.class );
61-
6259
private transient SharedSessionContractImplementor session;
6360
private boolean isTempSession = false;
6461

@@ -298,7 +295,7 @@ else if ( !session.isConnected() ) {
298295
tempSession.close();
299296
}
300297
catch (Exception e) {
301-
LOG.unableToCloseTemporarySession();
298+
COLLECTION_LOGGER.unableToCloseTemporarySession();
302299
}
303300
}
304301
else {
@@ -706,15 +703,15 @@ public final boolean unsetSession(SharedSessionContractImplementor currentSessio
706703
if ( allowLoadOutsideTransaction
707704
&& !initialized
708705
&& session.getLoadQueryInfluencers().hasEnabledFilters() ) {
709-
LOG.enabledFiltersWhenDetachFromSession( collectionInfoString( getRole(), getKey() ) );
706+
COLLECTION_LOGGER.enabledFiltersWhenDetachFromSession( collectionInfoString( getRole(), getKey() ) );
710707
}
711708
session = null;
712709
}
713710
return true;
714711
}
715712
else {
716713
if ( session != null ) {
717-
LOG.logCannotUnsetUnexpectedSessionInCollection( unexpectedSessionStateMessage( currentSession ) );
714+
COLLECTION_LOGGER.logCannotUnsetUnexpectedSessionInCollection( unexpectedSessionStateMessage( currentSession ) );
718715
}
719716
return false;
720717
}
@@ -724,20 +721,20 @@ private void logDiscardedQueuedOperations() {
724721
try {
725722
if ( wasTransactionRolledBack() ) {
726723
// It was due to a rollback.
727-
if ( LOG.isDebugEnabled()) {
728-
LOG.queuedOperationWhenDetachFromSessionOnRollback( collectionInfoString( getRole(), getKey() ) );
724+
if ( COLLECTION_LOGGER.isDebugEnabled()) {
725+
COLLECTION_LOGGER.queuedOperationWhenDetachFromSessionOnRollback( collectionInfoString( getRole(), getKey() ) );
729726
}
730727
}
731728
else {
732729
// We don't know why the collection is being detached.
733730
// Just log the info.
734-
LOG.queuedOperationWhenDetachFromSession( collectionInfoString( getRole(), getKey() ) );
731+
COLLECTION_LOGGER.queuedOperationWhenDetachFromSession( collectionInfoString( getRole(), getKey() ) );
735732
}
736733
}
737734
catch (Exception e) {
738735
// We don't know why the collection is being detached.
739736
// Just log the info.
740-
LOG.queuedOperationWhenDetachFromSession( collectionInfoString( getRole(), getKey() ) );
737+
COLLECTION_LOGGER.queuedOperationWhenDetachFromSession( collectionInfoString( getRole(), getKey() ) );
741738
}
742739
}
743740

@@ -772,11 +769,11 @@ else if ( this.session != null ) {
772769
);
773770
}
774771
else {
775-
LOG.logUnexpectedSessionInCollectionNotConnected( message );
772+
COLLECTION_LOGGER.logUnexpectedSessionInCollectionNotConnected( message );
776773
}
777774
}
778775
if ( hasQueuedOperations() ) {
779-
LOG.queuedOperationWhenAttachToSession( collectionInfoString( getRole(), getKey() ) );
776+
COLLECTION_LOGGER.queuedOperationWhenAttachToSession( collectionInfoString( getRole(), getKey() ) );
780777
}
781778
this.session = session;
782779
return true;
@@ -808,7 +805,7 @@ private String unexpectedSessionStateMessage(SharedSessionContractImplementor se
808805
}
809806
}
810807
// only include the collection contents if debug logging
811-
if ( LOG.isDebugEnabled() ) {
808+
if ( COLLECTION_LOGGER.isDebugEnabled() ) {
812809
final String collectionContents = wasInitialized() ? toString() : "<uninitialized>";
813810
message.append( "\nCollection contents: [" ).append( collectionContents ).append( "]" );
814811
}

0 commit comments

Comments
 (0)