Skip to content

Commit 6fee666

Browse files
committed
The pool notifies the connect handler after the connection has been created and inserted in the pool and before the first time it is used. This is implemented for connection acquisitions but not for scheduled commands.
The connect handler is now called after the connection is created and before it is inserted in the pool. As consequence it works universally for connection acquisition and scheduled commands. fixes eclipse-vertx#1134
1 parent 6ef2b6e commit 6fee666

File tree

2 files changed

+36
-27
lines changed

2 files changed

+36
-27
lines changed

vertx-pg-client/src/test/java/io/vertx/pgclient/PgPoolTest.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -425,21 +425,35 @@ public void testNoConnectionLeaks(TestContext ctx) {
425425
}
426426

427427
@Test
428-
public void testConnectionHook(TestContext ctx) {
429-
Async async = ctx.async();
428+
public void testConnectionHook1(TestContext ctx) {
429+
Async async = ctx.async(2);
430430
Handler<SqlConnection> hook = f -> {
431431
vertx.setTimer(1000, id -> {
432-
f.close();
432+
f.close().onComplete(ctx.asyncAssertSuccess(v -> async.countDown()));
433433
});
434434
};
435435
PgPool pool = createPool(options, new PoolOptions().setMaxSize(1)).connectHandler(hook);
436436
pool.getConnection(ctx.asyncAssertSuccess(conn -> {
437437
conn.query("SELECT id, randomnumber from WORLD").execute(ctx.asyncAssertSuccess(v2 -> {
438-
async.complete();
438+
async.countDown();
439439
}));
440440
}));
441441
}
442442

443+
@Test
444+
public void testConnectionHook2(TestContext ctx) {
445+
Async async = ctx.async(2);
446+
Handler<SqlConnection> hook = f -> {
447+
vertx.setTimer(1000, id -> {
448+
f.close().onComplete(ctx.asyncAssertSuccess(v -> async.countDown()));
449+
});
450+
};
451+
PgPool pool = createPool(options, new PoolOptions().setMaxSize(1)).connectHandler(hook);
452+
pool.query("SELECT id, randomnumber from WORLD").execute(ctx.asyncAssertSuccess(v2 -> {
453+
async.countDown();
454+
}));
455+
}
456+
443457
@Test
444458
public void testConnectionClosedInHook(TestContext ctx) {
445459
Async async = ctx.async(2);

vertx-sql-client/src/main/java/io/vertx/sqlclient/impl/pool/SqlConnectionPool.java

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,13 @@ public void connect(EventLoopContext context, PoolConnector.Listener listener, H
109109
if (conn.isValid()) {
110110
PooledConnection pooled = new PooledConnection(res.factory(), conn, listener);
111111
conn.init(pooled);
112-
handler.handle(Future.succeededFuture(new ConnectResult<>(pooled, pipeliningLimit, 0)));
112+
Handler<PooledConnection> connectionHandler = hook.get();
113+
if (connectionHandler != null) {
114+
pooled.poolResultHandler = handler;
115+
connectionHandler.handle(pooled);
116+
} else {
117+
handler.handle(Future.succeededFuture(new ConnectResult<>(pooled, pipeliningLimit, 0)));
118+
}
113119
} else {
114120
handler.handle(Future.failedFuture(ConnectionBase.CLOSED_EXCEPTION));
115121
}
@@ -168,6 +174,7 @@ public <R> Future<R> execute(ContextInternal context, CommandBase<R> cmd) {
168174
public void acquire(ContextInternal context, long timeout, Handler<AsyncResult<PooledConnection>> handler) {
169175
class PoolRequest implements PoolWaiter.Listener<PooledConnection>, Handler<AsyncResult<Lease<PooledConnection>>> {
170176
private long timerID = -1L;
177+
171178
@Override
172179
public void handle(AsyncResult<Lease<PooledConnection>> ar) {
173180
if (timerID != -1L) {
@@ -191,19 +198,10 @@ public void handle(AsyncResult<Lease<PooledConnection>> ar) {
191198
handler.handle(Future.failedFuture(ar.cause()));
192199
}
193200
}
201+
194202
private void handle(Lease<PooledConnection> lease) {
195203
PooledConnection pooled = lease.get();
196204
pooled.lease = lease;
197-
if (!pooled.initialized) {
198-
Handler<PooledConnection> connectionHandler = hook.get();
199-
if (connectionHandler != null) {
200-
pooled.continuation = handler;
201-
connectionHandler.handle(pooled);
202-
return;
203-
} else {
204-
pooled.initialized = true;
205-
}
206-
}
207205
handler.handle(Future.succeededFuture(pooled));
208206
}
209207

@@ -245,10 +243,9 @@ public class PooledConnection implements Connection, Connection.Holder {
245243
private final Connection conn;
246244
private final PoolConnector.Listener listener;
247245
private Holder holder;
246+
private Handler<AsyncResult<ConnectResult<PooledConnection>>> poolResultHandler;
248247
private Lease<PooledConnection> lease;
249248
public long expirationTimestamp;
250-
private boolean initialized;
251-
private Handler<AsyncResult<PooledConnection>> continuation;
252249

253250
PooledConnection(ConnectionFactory factory, Connection conn, PoolConnector.Listener listener) {
254251
this.factory = factory;
@@ -317,12 +314,11 @@ private void doClose(Holder holder, Promise<Void> promise) {
317314
promise.fail(msg);
318315
} else {
319316
this.holder = null;
320-
if (!initialized) {
321-
initialized = true;
322-
Handler<AsyncResult<PooledConnection>> c = continuation;
323-
continuation = null;
317+
Handler<AsyncResult<ConnectResult<PooledConnection>>> resultHandler = poolResultHandler;
318+
if (resultHandler != null) {
319+
poolResultHandler = null;
324320
promise.complete();
325-
c.handle(Future.succeededFuture(this));
321+
resultHandler.handle(Future.succeededFuture(new ConnectResult<>(this, pipeliningLimit, 0)));
326322
return;
327323
}
328324
if (beforeRecycle == null) {
@@ -346,11 +342,10 @@ public void handleClosed() {
346342
if (holder != null) {
347343
holder.handleClosed();
348344
}
349-
350-
Handler<AsyncResult<PooledConnection>> c = this.continuation;
351-
if (c != null) {
352-
this.continuation = null;
353-
c.handle(Future.failedFuture(ConnectionBase.CLOSED_EXCEPTION));
345+
Handler<AsyncResult<ConnectResult<PooledConnection>>> resultHandler = poolResultHandler;
346+
if (resultHandler != null) {
347+
poolResultHandler = null;
348+
resultHandler.handle(Future.failedFuture(ConnectionBase.CLOSED_EXCEPTION));
354349
}
355350
listener.onRemove();
356351
}

0 commit comments

Comments
 (0)