5
5
*/
6
6
package org .hibernate .reactive .pool .impl ;
7
7
8
+ import java .util .concurrent .CompletableFuture ;
8
9
import java .util .concurrent .CompletionStage ;
10
+ import java .util .function .Consumer ;
9
11
10
12
import org .hibernate .engine .jdbc .spi .SqlExceptionHelper ;
11
13
import org .hibernate .engine .jdbc .spi .SqlStatementLogger ;
12
14
import org .hibernate .reactive .pool .ReactiveConnection ;
13
15
import org .hibernate .reactive .pool .ReactiveConnectionPool ;
14
16
17
+ import io .vertx .core .Future ;
15
18
import io .vertx .sqlclient .Pool ;
16
19
import io .vertx .sqlclient .SqlConnection ;
17
20
@@ -41,13 +44,13 @@ public abstract class SqlClientPool implements ReactiveConnectionPool {
41
44
42
45
/**
43
46
* @return a Hibernate {@link SqlStatementLogger} for logging SQL
44
- * statements as they are executed
47
+ * statements as they are executed
45
48
*/
46
49
protected abstract SqlStatementLogger getSqlStatementLogger ();
47
50
48
51
/**
49
52
* @return a Hibernate {@link SqlExceptionHelper} for converting
50
- * exceptions
53
+ * exceptions
51
54
*/
52
55
protected abstract SqlExceptionHelper getSqlExceptionHelper ();
53
56
@@ -58,9 +61,7 @@ public abstract class SqlClientPool implements ReactiveConnectionPool {
58
61
* subclasses which support multitenancy.
59
62
*
60
63
* @param tenantId the id of the tenant
61
- *
62
64
* @throws UnsupportedOperationException if multitenancy is not supported
63
- *
64
65
* @see ReactiveConnectionPool#getConnection(String)
65
66
*/
66
67
protected Pool getTenantPool (String tenantId ) {
@@ -88,13 +89,33 @@ public CompletionStage<ReactiveConnection> getConnection(String tenantId, SqlExc
88
89
}
89
90
90
91
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 );
93
93
}
94
94
95
95
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 ;
98
119
}
99
120
100
121
private SqlClientConnection newConnection (SqlConnection connection ) {
0 commit comments