Skip to content

Commit e4fdfd4

Browse files
committed
[#2768] Help tracking the connection creator
We create an exception stack when we open a new connection, so that we can log the stack trace and track more easily which method we are using in case of error.
1 parent 9dce31a commit e4fdfd4

File tree

4 files changed

+19
-11
lines changed

4 files changed

+19
-11
lines changed

hibernate-reactive-core/src/main/java/org/hibernate/reactive/common/InternalStateAssertions.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,12 @@ public static void assertCurrentThreadMatches(Thread expectedThread) {
5454
}
5555
}
5656

57-
public static void assertCurrentContextMatches(Object object, ContextInternal expectedContext) {
57+
public static void assertCurrentContextMatches(Object object, ContextInternal expectedContext, Exception creationTrace) {
5858
if ( ENFORCE ) {
5959
final ContextInternal currentContext = ContextInternal.current();
6060
Objects.requireNonNull( currentContext, "Current context cannot be null" );
6161
if ( !currentContext.equals( expectedContext ) ) {
62-
throw LOG.unexpectedContextDetected( object, expectedContext, currentContext );
62+
throw LOG.unexpectedContextDetected( object, expectedContext, currentContext, creationTrace );
6363
}
6464
}
6565
}

hibernate-reactive-core/src/main/java/org/hibernate/reactive/logging/impl/Log.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,9 @@ public interface Log extends BasicLogger {
274274
@Message(id = 86, value = "Error closing reactive connection")
275275
void errorClosingConnection(@Cause Throwable throwable);
276276

277+
// createStack will help in tracking which method created the object
277278
@Message(id = 88, value = "Expected to use the object %1$s on context %2$s but was %3$s")
278-
HibernateException unexpectedContextDetected(Object obj, ContextInternal expectedContext, ContextInternal currentContext);
279+
HibernateException unexpectedContextDetected(Object obj, ContextInternal expectedContext, ContextInternal currentContext, @Cause Exception createStack);
279280

280281
// Same method that exists in CoreMessageLogger
281282
@LogMessage(level = WARN)

hibernate-reactive-core/src/main/java/org/hibernate/reactive/pool/impl/SqlClientConnection.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public class SqlClientConnection implements ReactiveConnection {
5858

5959
private final SqlStatementLogger sqlStatementLogger;
6060
private final SqlExceptionHelper sqlExceptionHelper;
61+
private final Exception creationTrace;
6162

6263
private final Pool pool;
6364
private final SqlConnection connection;
@@ -70,12 +71,15 @@ public class SqlClientConnection implements ReactiveConnection {
7071
Pool pool,
7172
SqlStatementLogger sqlStatementLogger,
7273
SqlExceptionHelper sqlExceptionHelper,
73-
ContextInternal connectionContext) {
74+
ContextInternal connectionContext,
75+
// Helps to figure out where and when the connection was created
76+
Exception creationTrace) {
7477
this.connectionContext = connectionContext;
7578
this.pool = pool;
7679
this.sqlStatementLogger = sqlStatementLogger;
7780
this.connection = connection;
7881
this.sqlExceptionHelper = sqlExceptionHelper;
82+
this.creationTrace = creationTrace;
7983
LOG.tracef( "Connection created for %1$s associated to context %2$s: ", connection, connectionContext );
8084
}
8185

@@ -348,7 +352,7 @@ public CompletionStage<RowSet<Row>> preparedQueryOutsideTransaction(String sql)
348352
}
349353

350354
private void feedback(String sql) {
351-
InternalStateAssertions.assertCurrentContextMatches( this, connectionContext );
355+
InternalStateAssertions.assertCurrentContextMatches( this, connectionContext, creationTrace );
352356
Objects.requireNonNull( sql, "SQL query cannot be null" );
353357
// DDL already gets formatted by the client, so don't reformat it
354358
FormatStyle formatStyle = sqlStatementLogger.isFormat() && !sql.contains( System.lineSeparator() )

hibernate-reactive-core/src/main/java/org/hibernate/reactive/pool/impl/SqlClientPool.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,16 +125,18 @@ public CompletionStage<ReactiveConnection> getConnection(String tenantId, SqlExc
125125
}
126126

127127
private CompletionStage<ReactiveConnection> getConnectionFromPool(Pool pool) {
128+
final Exception creationTrace = new Exception();
128129
return completeFuture(
129-
pool.getConnection().map( this::newConnection ),
130+
pool.getConnection().map( connection -> newConnection( connection, creationTrace ) ),
130131
ReactiveConnection::close
131132
);
132133
}
133134

134135
private CompletionStage<ReactiveConnection> getConnectionFromPool(Pool pool, SqlExceptionHelper sqlExceptionHelper) {
136+
final Exception creationTrace = new Exception();
135137
return completeFuture(
136138
pool.getConnection()
137-
.map( sqlConnection -> newConnection( sqlConnection, sqlExceptionHelper ) ),
139+
.map( sqlConnection -> newConnection( sqlConnection, sqlExceptionHelper, creationTrace ) ),
138140
ReactiveConnection::close
139141
);
140142
}
@@ -211,17 +213,18 @@ private <T> CompletionStage<T> completeFuture(Future<T> future, Consumer<T> onCa
211213
return completableFuture;
212214
}
213215

214-
private SqlClientConnection newConnection(SqlConnection connection) {
215-
return newConnection( connection, getSqlExceptionHelper() );
216+
private SqlClientConnection newConnection(SqlConnection connection, Exception creationTrace) {
217+
return newConnection( connection, getSqlExceptionHelper(), creationTrace );
216218
}
217219

218-
private SqlClientConnection newConnection(SqlConnection connection, SqlExceptionHelper sqlExceptionHelper) {
220+
private SqlClientConnection newConnection(SqlConnection connection, SqlExceptionHelper sqlExceptionHelper, Exception creationTrace) {
219221
return new SqlClientConnection(
220222
connection,
221223
getPool(),
222224
getSqlStatementLogger(),
223225
sqlExceptionHelper,
224-
ContextInternal.current()
226+
ContextInternal.current(),
227+
creationTrace
225228
);
226229
}
227230

0 commit comments

Comments
 (0)