Repository: eclipse-vertx/vertx-sql-client
Issue: Pool implementation for one-shot queries does not use all available resources

When executing one-shot queries, the pool implementation will often schedule the SQL commands on the few available connections instead of creating more if maximum size has not been reached.

Consider the following test:

```java
  @Test
  public void testUseAvailableResources(TestContext ctx) {
    int poolSize = 10;
    Async async = ctx.async();
    PgPool pool = PgPool.pool(options, new PoolOptions().setMaxSize(poolSize));
    AtomicReference<PgConnection> ctrlConnRef = new AtomicReference<>();
    PgConnection.connect(vertx, options, ctx.asyncAssertSuccess(ctrlConn -> {
      ctrlConnRef.set(ctrlConn);
      for (int i = 0; i < poolSize; i++) {
        vertx.setTimer(10 * (i + 1), l -> {
          pool.query("select pg_sleep(5)").execute(ar -> {});
        });
      }
      vertx.setTimer(10 * (poolSize + 1), event -> {
        ctrlConn.query("select count(*) as cnt from pg_stat_activity where application_name like '%vertx%'").execute(ctx.asyncAssertSuccess(rows -> {
          Integer count = rows.iterator().next().getInteger("cnt");
          ctx.assertEquals(poolSize + 1, count);
          async.complete();
        }));
      });
    }));
    try {
      async.await();
    } finally {
      PgConnection ctrlConn = ctrlConnRef.get();
      if (ctrlConn != null) {
        ctrlConn.close();
      }
      pool.close();
    }
  }
```

It fails like this:

```
java.lang.AssertionError: Not equals : 11 != 2
```

This test targets Postgres but the problem is in `PoolBase` implementation so I believe it affects all SQL clients.



Changed files:
vertx-pg-client/src/test/java/io/vertx/pgclient/PgPoolTest.java, vertx-sql-client/src/main/java/io/vertx/sqlclient/impl/PoolBase.java

diff --git a/vertx-pg-client/src/test/java/io/vertx/pgclient/PgPoolTest.java b/vertx-pg-client/src/test/java/io/vertx/pgclient/PgPoolTest.java
index 67bc6faf..e9ad6a19 100644
--- a/vertx-pg-client/src/test/java/io/vertx/pgclient/PgPoolTest.java
+++ b/vertx-pg-client/src/test/java/io/vertx/pgclient/PgPoolTest.java
@@ -17,9 +17,9 @@
 
 package io.vertx.pgclient;
 
-import io.vertx.sqlclient.PoolOptions;
 import io.vertx.ext.unit.Async;
 import io.vertx.ext.unit.TestContext;
+import io.vertx.sqlclient.PoolOptions;
 import org.junit.Test;
 
 import java.util.concurrent.atomic.AtomicReference;
@@ -130,4 +130,36 @@ public class PgPoolTest extends PgPoolTestBase {
       pool.close();
     }
   }
+
+  @Test
+  public void testUseAvailableResources(TestContext ctx) {
+    int poolSize = 10;
+    Async async = ctx.async(poolSize + 1);
+    PgPool pool = PgPool.pool(options, new PoolOptions().setMaxSize(poolSize));
+    AtomicReference<PgConnection> ctrlConnRef = new AtomicReference<>();
+    PgConnection.connect(vertx, options, ctx.asyncAssertSuccess(ctrlConn -> {
+      ctrlConnRef.set(ctrlConn);
+      for (int i = 0; i < poolSize; i++) {
+        vertx.setTimer(10 * (i + 1), l -> {
+          pool.query("select pg_sleep(5)").execute(ctx.asyncAssertSuccess(res -> async.countDown()));
+        });
+      }
+      vertx.setTimer(10 * (poolSize + 1), event -> {
+        ctrlConn.query("select count(*) as cnt from pg_stat_activity where application_name like '%vertx%'").execute(ctx.asyncAssertSuccess(rows -> {
+          Integer count = rows.iterator().next().getInteger("cnt");
+          ctx.assertEquals(poolSize + 1, count);
+          async.countDown();
+        }));
+      });
+    }));
+    try {
+      async.await();
+    } finally {
+      PgConnection ctrlConn = ctrlConnRef.get();
+      if (ctrlConn != null) {
+        ctrlConn.close();
+      }
+      pool.close();
+    }
+  }
 }
diff --git a/vertx-sql-client/src/main/java/io/vertx/sqlclient/impl/PoolBase.java b/vertx-sql-client/src/main/java/io/vertx/sqlclient/impl/PoolBase.java
index 35874ccb..2fe4ec9f 100644
--- a/vertx-sql-client/src/main/java/io/vertx/sqlclient/impl/PoolBase.java
+++ b/vertx-sql-client/src/main/java/io/vertx/sqlclient/impl/PoolBase.java
@@ -17,19 +17,13 @@
 
 package io.vertx.sqlclient.impl;
 
-import io.vertx.sqlclient.PoolOptions;
+import io.vertx.core.*;
 import io.vertx.sqlclient.Pool;
+import io.vertx.sqlclient.PoolOptions;
 import io.vertx.sqlclient.SqlConnection;
 import io.vertx.sqlclient.Transaction;
 import io.vertx.sqlclient.impl.command.CommandBase;
 import io.vertx.sqlclient.impl.command.CommandResponse;
-import io.vertx.sqlclient.impl.command.CommandScheduler;
-import io.vertx.core.AsyncResult;
-import io.vertx.core.Context;
-import io.vertx.core.Future;
-import io.vertx.core.Handler;
-import io.vertx.core.Vertx;
-import io.vertx.core.VertxException;
 
 /**
  * Todo :
@@ -88,9 +82,11 @@ public abstract class PoolBase<P extends PoolBase<P>> extends SqlClientBase<P> i
       pool.acquire(new CommandWaiter() { // SHOULD BE IT !!!!!
         @Override
         protected void onSuccess(Connection conn) {
-          cmd.handler = handler;
+          cmd.handler = commandResponse -> {
+            conn.close(this);
+            handler.handle(commandResponse);
+          };
           conn.schedule(cmd);
-          conn.close(this);
         }
         @Override
         protected void onFailure(Throwable cause) {