Skip to content

Commit 6e8d1e2

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 6e8d1e2

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
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: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@
135135
import static org.hibernate.reactive.persister.entity.impl.ReactiveEntityPersister.forceInitialize;
136136
import static org.hibernate.reactive.session.impl.SessionUtil.checkEntityFound;
137137
import static org.hibernate.reactive.util.impl.CompletionStages.completedFuture;
138+
import static org.hibernate.reactive.util.impl.CompletionStages.failedFuture;
138139
import static org.hibernate.reactive.util.impl.CompletionStages.nullFuture;
139140
import static org.hibernate.reactive.util.impl.CompletionStages.rethrow;
140141
import static org.hibernate.reactive.util.impl.CompletionStages.returnNullorRethrow;
@@ -963,7 +964,7 @@ public CompletionStage<Void> reactiveForceFlush(EntityEntry entry) {
963964
}
964965

965966
if ( getPersistenceContextInternal().getCascadeLevel() > 0 ) {
966-
return CompletionStages.failedFuture( new ObjectDeletedException(
967+
return failedFuture( new ObjectDeletedException(
967968
"deleted object would be re-saved by cascade (remove deleted object from associations)",
968969
entry.getId(),
969970
entry.getPersister().getEntityName()
@@ -1616,7 +1617,23 @@ public void close() throws HibernateException {
16161617

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

0 commit comments

Comments
 (0)