21
21
import javax .annotation .concurrent .GuardedBy ;
22
22
import java .net .InetSocketAddress ;
23
23
import java .nio .charset .Charset ;
24
+ import java .util .ArrayList ;
25
+ import java .util .Collection ;
24
26
import java .util .LinkedHashMap ;
25
27
import java .util .LinkedList ;
26
28
import java .util .Map ;
31
33
import java .util .function .BiConsumer ;
32
34
import java .util .function .Consumer ;
33
35
import java .util .function .Function ;
36
+ import java .util .logging .Level ;
37
+ import java .util .logging .Logger ;
38
+ import java .util .stream .Collectors ;
34
39
35
40
/**
36
41
* Pool for backend connections.
@@ -106,16 +111,24 @@ boolean isConnected() {
106
111
return delegate .isConnected ();
107
112
}
108
113
109
- void shutdown () {
110
- statements .forEach ((sql , stmt ) -> stmt .delegate .close ().join ());
114
+ CompletableFuture <Void > shutdown () {
115
+ CompletableFuture <?>[] closeTasks = statements .values ().stream ()
116
+ .map (stmt -> stmt .delegate .close ())
117
+ .collect (Collectors .toList ()).toArray (new CompletableFuture <?>[]{});
111
118
statements .clear ();
112
- delegate .close ().join ();
119
+ return CompletableFuture .allOf (closeTasks )
120
+ .thenApply (v -> {
121
+ if (!statements .isEmpty ()) {
122
+ Logger .getLogger (PooledPgConnection .class .getName ()).log (Level .WARNING , "Stale prepared statements detected {0}" , statements .size ());
123
+ }
124
+ return delegate .close ();
125
+ })
126
+ .thenCompose (Function .identity ());
113
127
}
114
128
115
129
@ Override
116
130
public CompletableFuture <Void > close () {
117
- release (this );
118
- return CompletableFuture .completedFuture (null );
131
+ return release (this );
119
132
}
120
133
121
134
@ Override
@@ -222,6 +235,7 @@ public PgConnectionPool(PoolProperties properties, Executor futuresExecutor) {
222
235
223
236
@ Override
224
237
public CompletableFuture <Void > close () {
238
+ Collection <CompletableFuture <Void >> shutdownTasks = new ArrayList <>();
225
239
lock .lock ();
226
240
try {
227
241
closed = true ;
@@ -231,13 +245,13 @@ public CompletableFuture<Void> close() {
231
245
}
232
246
while (!connections .isEmpty ()) {
233
247
PooledPgConnection connection = connections .poll ();
234
- connection .shutdown ();
248
+ shutdownTasks . add ( connection .shutdown () );
235
249
size --;
236
250
}
237
251
} finally {
238
252
lock .unlock ();
239
253
}
240
- return CompletableFuture .completedFuture ( null );
254
+ return CompletableFuture .allOf ( shutdownTasks . toArray ( size -> new CompletableFuture <?>[ size ]) );
241
255
}
242
256
243
257
@ Override
@@ -297,15 +311,16 @@ private boolean tryIncreaseSize() {
297
311
}
298
312
}
299
313
300
- private void release (PooledPgConnection connection ) {
314
+ private CompletableFuture < Void > release (PooledPgConnection connection ) {
301
315
if (connection == null ) {
302
- throw new IllegalArgumentException ("'connection' should be non null to be returned to the pool " );
316
+ throw new IllegalArgumentException ("'connection' should be not null" );
303
317
}
318
+ CompletableFuture <Void > shutdownTask = CompletableFuture .completedFuture (null );
304
319
lock .lock ();
305
320
try {
306
321
if (closed ) {
307
322
if (connection .isConnected ()) {
308
- connection .shutdown ();
323
+ shutdownTask = connection .shutdown ();
309
324
}
310
325
} else {
311
326
if (connection .isConnected ()) {
@@ -321,6 +336,7 @@ private void release(PooledPgConnection connection) {
321
336
} finally {
322
337
lock .unlock ();
323
338
}
339
+ return shutdownTask ;
324
340
}
325
341
326
342
@ Override
0 commit comments