@@ -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