Skip to content

Commit 002e56a

Browse files
stuartwdouglasDavideD
authored andcommitted
Improve ProxyConnection error handling
- Add better error message if it has been closed - Don't explicity throw the exception, return a failed CS instead
1 parent 65ff89a commit 002e56a

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

hibernate-reactive-core/src/main/java/org/hibernate/reactive/pool/impl/ProxyConnection.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import java.sql.ResultSet;
99
import java.util.List;
10+
import java.util.concurrent.CompletableFuture;
1011
import java.util.concurrent.CompletionStage;
1112
import java.util.function.Function;
1213

@@ -24,6 +25,7 @@ final class ProxyConnection implements ReactiveConnection {
2425
private final ReactiveConnectionPool sqlClientPool;
2526
private ReactiveConnection connection;
2627
private boolean connected;
28+
private boolean closed;
2729
private final String tenantId;
2830

2931
public ProxyConnection(ReactiveConnectionPool sqlClientPool) {
@@ -38,6 +40,11 @@ public ProxyConnection(ReactiveConnectionPool sqlClientPool, String tenantId) {
3840

3941
private <T> CompletionStage<T> withConnection(Function<ReactiveConnection, CompletionStage<T>> operation) {
4042
assertUseOnEventLoop();
43+
if ( closed ) {
44+
CompletableFuture<T> ret = new CompletableFuture<>();
45+
ret.completeExceptionally( new IllegalStateException( "session is closed" ) );
46+
return ret;
47+
}
4148
if ( !connected ) {
4249
connected = true; // we're not allowed to fetch two connections!
4350
CompletionStage<ReactiveConnection> connection =
@@ -49,7 +56,9 @@ private <T> CompletionStage<T> withConnection(Function<ReactiveConnection, Compl
4956
if ( connection == null ) {
5057
// we're already in the process of fetching a connection,
5158
// so this must be an illegal concurrent call
52-
throw new IllegalStateException( "session is currently connecting to database" );
59+
CompletableFuture<T> ret = new CompletableFuture<>();
60+
ret.completeExceptionally( new IllegalStateException( "session is currently connecting to database" ) );
61+
return ret;
5362
}
5463
return operation.apply( connection );
5564
}
@@ -140,6 +149,7 @@ public void close() {
140149
connection.close();
141150
connection = null;
142151
}
152+
closed = true;
143153
}
144154

145155
}

0 commit comments

Comments
 (0)