本文介绍了Vertx 连接超时未捕获 JDBCClient (.getConnection)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I can't handle case when connection failed in JDBCClient, example: no host to route, connection time out and etc. Because the method .getConnection() is not return failedFuture but it show cause in monitor then it's will silence.I think it should send the fail future more than print error log when connection time out or other cause.

我的示例代码是.

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

我在日志中收到此错误:

i got this Error in logs:

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

有人可以帮助我.

谢谢.

推荐答案

你可以尝试做这样的事情:

You can try to make something like this:

  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;
  }

只需手动创建未来,然后在回调中解决或拒绝它.对于这种情况,这实际上是一种常见的方法.

Just create future manually and then resolve or reject it in the callback. It's actually common approach for such cases.

这篇关于Vertx 连接超时未捕获 JDBCClient (.getConnection)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 08:40