Skip to content

Commit dd8e520

Browse files
committed
[#2699] Make sure to close the connection in case of error
I've tested it while working on #2518, but I don't know how to create an isolated test.
1 parent ad96cc4 commit dd8e520

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,10 @@ public interface Log extends BasicLogger {
273273
@Message(id = 85, value = "Reactive sessions do not support transparent lazy fetching - use Stage.fetch() or Mutiny.fetch() (property '%1$S' of entity '%2$s' was not loaded)")
274274
LazyInitializationException lazyFieldInitializationException(String fieldName, String entityName);
275275

276+
@LogMessage(level = ERROR)
277+
@Message(id = 86, value = "Error closing reactive connection")
278+
void errorClosingConnection(@Cause Throwable throwable);
279+
276280
// Same method that exists in CoreMessageLogger
277281
@LogMessage(level = WARN)
278282
@Message(id = 104, value = "firstResult/maxResults specified with collection fetch; applying in memory!" )
@@ -331,5 +335,4 @@ public interface Log extends BasicLogger {
331335
" This is probably due to an operation failing fast due to the transaction being marked for rollback.",
332336
id = 520)
333337
void jdbcExceptionThrownWithTransactionRolledBack(@Cause JDBCException e);
334-
335338
}

hibernate-reactive-core/src/main/java/org/hibernate/reactive/session/impl/ReactiveSessionImpl.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1738,7 +1738,23 @@ public void close() throws HibernateException {
17381738

17391739
@Override
17401740
public CompletionStage<Void> reactiveClose() {
1741-
super.close();
1741+
try {
1742+
super.close();
1743+
return closeConnection();
1744+
}
1745+
catch (RuntimeException e) {
1746+
return closeConnection()
1747+
.handle( CompletionStages::handle )
1748+
.thenCompose( closeConnectionHandler -> {
1749+
if ( closeConnectionHandler.hasFailed() ) {
1750+
LOG.errorClosingConnection( closeConnectionHandler.getThrowable() );
1751+
}
1752+
return failedFuture( e );
1753+
} );
1754+
}
1755+
}
1756+
1757+
private CompletionStage<Void> closeConnection() {
17421758
return reactiveConnection != null
17431759
? reactiveConnection.close()
17441760
: voidFuture();

0 commit comments

Comments
 (0)