Skip to content

Commit aed8402

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 2d45284 commit aed8402

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
@@ -270,6 +270,10 @@ public interface Log extends BasicLogger {
270270
@Message(id = 84, value = "The application requested a JDBC connection, but Hibernate Reactive doesn't use JDBC. This could be caused by a bug or the use of an unsupported feature in Hibernate Reactive")
271271
SQLException notUsingJdbc();
272272

273+
@LogMessage(level = ERROR)
274+
@Message(id = 86, value = "Error closing reactive connection")
275+
void errorClosingConnection(@Cause Throwable throwable);
276+
273277
// Same method that exists in CoreMessageLogger
274278
@LogMessage(level = WARN)
275279
@Message(id = 104, value = "firstResult/maxResults specified with collection fetch; applying in memory!" )
@@ -328,5 +332,4 @@ public interface Log extends BasicLogger {
328332
" This is probably due to an operation failing fast due to the transaction being marked for rollback.",
329333
id = 520)
330334
void jdbcExceptionThrownWithTransactionRolledBack(@Cause JDBCException e);
331-
332335
}

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

17011701
@Override
17021702
public CompletionStage<Void> reactiveClose() {
1703-
super.close();
1703+
try {
1704+
super.close();
1705+
return closeConnection();
1706+
}
1707+
catch (RuntimeException e) {
1708+
return closeConnection()
1709+
.handle( CompletionStages::handle )
1710+
.thenCompose( closeConnectionHandler -> {
1711+
if ( closeConnectionHandler.hasFailed() ) {
1712+
LOG.errorClosingConnection( closeConnectionHandler.getThrowable() );
1713+
}
1714+
return failedFuture( e );
1715+
} );
1716+
}
1717+
}
1718+
1719+
private CompletionStage<Void> closeConnection() {
17041720
return reactiveConnection != null
17051721
? reactiveConnection.close()
17061722
: voidFuture();

0 commit comments

Comments
 (0)