gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 00:33:51 28 4
gpt4 key购买 nike

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

我的示例代码是。

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

请有人帮助我。

谢谢。

最佳答案

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

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

只需手动创建 future,然后在回调中解决或拒绝它。这实际上是此类情况的常见方法。

关于java - Vertx 连接超时未捕获 JDBCClient (.getConnection),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47264092/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com