Vertx连接超时未捕获JDBCClient(.getConnection)

时间:2017-11-13 12:11:02

标签: java vert.x

我无法处理JDBCClient中连接失败的情况,例如:没有主机路由,连接超时等等。因为方法.getConnection()没有返回failedFuture但它在监视器中显示原因然后它将静默。 我认为它应该在连接超时或其他原因时发送失败的未来而不是打印错误日志。

我的示例代码是。

JDBCClient client = ...;
client.getConnection(conn -> {
    if (conn.succeeded()) {
        ....
    } else {
        // This is never executed (connection time out and etc.)
        handler.handle(Future.failedFuture(conn.cause()));
    }
});

我在日志中遇到此错误:

An attempt by a client to checkout a Connection has timed out.
java.sql.SQLException: An attempt by a client to checkout a Connection has timed out.
    at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:118) 
    at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:77) 
    at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:690) 
    at com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.getConnection(AbstractPoolBackedDataSource.java:140) 
    at JDBCClient.lambda$getConnection$1(JDBCClient.java:132) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) ~[?:1.8.0_144]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) ~[?:1.8.0_144]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_144]
Caused by: com.mchange.v2.resourcepool.TimeoutException: A client timed out while waiting to acquire a resource from com.mchange.v2.resourcepool.BasicResourcePool@16404129 -- timeout at awaitAvailable()
    at com.mchange.v2.resourcepool.BasicResourcePool.awaitAvailable(BasicResourcePool.java:1467) 
    at com.mchange.v2.resourcepool.BasicResourcePool.prelimCheckoutResource(BasicResourcePool.java:644) 
    at com.mchange.v2.resourcepool.BasicResourcePool.checkoutResource(BasicResourcePool.java:554) 
    at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutAndMarkConnectionInUse(C3P0PooledConnectionPool.java:758) 
    at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:685) 
    ... 5 more

有人可以帮助我。

谢谢。

1 个答案:

答案 0 :(得分:0)

您可以尝试制作以下内容:

  public Future<SQLConnection> tryConnect() {
    JDBCClient client = JDBCClient.createNonShared(vertx, config());
    Future<SQLConnection> future = Future.future();
    client.getConnection(it -> {
      if (it.succeeded()) {
        future.complete(it.result());
      } else {
        future.fail(it.cause());
      }
    });
    return future;
  }

只需手动创建未来,然后在回调中解析或拒绝它。这种情况实际上是常见的做法。