55 */
66package org .hibernate .reactive .pool .impl ;
77
8+ import java .util .concurrent .CompletableFuture ;
89import java .util .concurrent .CompletionStage ;
10+ import java .util .function .Consumer ;
911
1012import org .hibernate .engine .jdbc .spi .SqlExceptionHelper ;
1113import org .hibernate .engine .jdbc .spi .SqlStatementLogger ;
1214import org .hibernate .reactive .pool .ReactiveConnection ;
1315import org .hibernate .reactive .pool .ReactiveConnectionPool ;
1416
17+ import io .vertx .core .Future ;
1518import io .vertx .sqlclient .Pool ;
1619import io .vertx .sqlclient .SqlConnection ;
1720
@@ -41,13 +44,13 @@ public abstract class SqlClientPool implements ReactiveConnectionPool {
4144
4245 /**
4346 * @return a Hibernate {@link SqlStatementLogger} for logging SQL
44- * statements as they are executed
47+ * statements as they are executed
4548 */
4649 protected abstract SqlStatementLogger getSqlStatementLogger ();
4750
4851 /**
4952 * @return a Hibernate {@link SqlExceptionHelper} for converting
50- * exceptions
53+ * exceptions
5154 */
5255 protected abstract SqlExceptionHelper getSqlExceptionHelper ();
5356
@@ -58,9 +61,7 @@ public abstract class SqlClientPool implements ReactiveConnectionPool {
5861 * subclasses which support multitenancy.
5962 *
6063 * @param tenantId the id of the tenant
61- *
6264 * @throws UnsupportedOperationException if multitenancy is not supported
63- *
6465 * @see ReactiveConnectionPool#getConnection(String)
6566 */
6667 protected Pool getTenantPool (String tenantId ) {
@@ -88,13 +89,33 @@ public CompletionStage<ReactiveConnection> getConnection(String tenantId, SqlExc
8889 }
8990
9091 private CompletionStage <ReactiveConnection > getConnectionFromPool (Pool pool ) {
91- return pool .getConnection ()
92- .toCompletionStage ().thenApply ( this ::newConnection );
92+ return completionStage ( pool .getConnection ().map ( this ::newConnection ), ReactiveConnection ::close );
9393 }
9494
9595 private CompletionStage <ReactiveConnection > getConnectionFromPool (Pool pool , SqlExceptionHelper sqlExceptionHelper ) {
96- return pool .getConnection ()
97- .toCompletionStage ().thenApply ( sqlConnection -> newConnection ( sqlConnection , sqlExceptionHelper ) );
96+ return completionStage (
97+ pool .getConnection ().map ( sqlConnection -> newConnection ( sqlConnection , sqlExceptionHelper ) ),
98+ ReactiveConnection ::close
99+ );
100+ }
101+
102+ /**
103+ * @param onCancellation invoke when converted {@link java.util.concurrent.CompletionStage} cancellation.
104+ */
105+ private <T > CompletionStage <T > completionStage (Future <T > future , Consumer <T > onCancellation ) {
106+ CompletableFuture <T > completableFuture = new CompletableFuture <>();
107+ future .onComplete ( ar -> {
108+ if ( ar .succeeded () ) {
109+ if ( completableFuture .isCancelled () ) {
110+ onCancellation .accept ( ar .result () );
111+ }
112+ completableFuture .complete ( ar .result () );
113+ }
114+ else {
115+ completableFuture .completeExceptionally ( ar .cause () );
116+ }
117+ } );
118+ return completableFuture ;
98119 }
99120
100121 private SqlClientConnection newConnection (SqlConnection connection ) {
0 commit comments