问题
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.
My example code is.
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
Someone can help me please.
thank you.
回答1:
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.
来源:https://stackoverflow.com/questions/47264092/vertx-connection-timeout-not-catched-jdbcclient-getconnection