Skip to content

Commit af1062e

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 7a0ca40 commit af1062e

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
@@ -1728,7 +1728,23 @@ public void close() throws HibernateException {
17281728

17291729
@Override
17301730
public CompletionStage<Void> reactiveClose() {
1731-
super.close();
1731+
try {
1732+
super.close();
1733+
return closeConnection();
1734+
}
1735+
catch (RuntimeException e) {
1736+
return closeConnection()
1737+
.handle( CompletionStages::handle )
1738+
.thenCompose( closeConnectionHandler -> {
1739+
if ( closeConnectionHandler.hasFailed() ) {
1740+
LOG.errorClosingConnection( closeConnectionHandler.getThrowable() );
1741+
}
1742+
return failedFuture( e );
1743+
} );
1744+
}
1745+
}
1746+
1747+
private CompletionStage<Void> closeConnection() {
17321748
return reactiveConnection != null
17331749
? reactiveConnection.close()
17341750
: voidFuture();

0 commit comments

Comments
 (0)