Skip to content

Commit 335e6f4

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 0e31b72 commit 335e6f4

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,10 @@ public interface Log extends BasicLogger {
262262
@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")
263263
SQLException notUsingJdbc();
264264

265+
@LogMessage(level = ERROR)
266+
@Message(id = 86, value = "Error closing reactive connection")
267+
void errorClosingConnection(@Cause Throwable throwable);
268+
265269
// Same method that exists in CoreMessageLogger
266270
@LogMessage(level = WARN)
267271
@Message(id = 104, value = "firstResult/maxResults specified with collection fetch; applying in memory!" )

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

16171617
@Override
16181618
public CompletionStage<Void> reactiveClose() {
1619-
super.close();
1619+
try {
1620+
super.close();
1621+
return closeConnection();
1622+
}
1623+
catch (RuntimeException e) {
1624+
return closeConnection()
1625+
.handle( CompletionStages::handle )
1626+
.thenCompose( closeConnectionHandler -> {
1627+
if ( closeConnectionHandler.hasFailed() ) {
1628+
LOG.errorClosingConnection( closeConnectionHandler.getThrowable() );
1629+
}
1630+
return failedFuture( e );
1631+
} );
1632+
}
1633+
}
1634+
1635+
private CompletionStage<Void> closeConnection() {
16201636
return reactiveConnection != null
16211637
? reactiveConnection.close()
16221638
: voidFuture();

0 commit comments

Comments
 (0)