[英]Check if Vert.x MySQL Pool initialized
我是 Vert.x 的新手,我想使用它的 MySQL 池。
這是我的設置:
MySQLConnectOptions connectOptions = new MySQLConnectOptions()
.setHost(hostname)
.setPort(port)
.setDatabase(dbname)
.setUser(user)
.setPassword(password)
.setCachePreparedStatements(true)
.setPreparedStatementCacheMaxSize(250)
.setPreparedStatementCacheSqlLimit(2048)
.setReconnectAttempts(1)
.setReconnectInterval(10);
PoolOptions poolOptions = new PoolOptions().setMaxSize(5);
pool = MySQLPool.pool(vertx, connectOptions, poolOptions);
pool.connectHandler(handler -> {
Future<Transaction> begin = handler.begin();
if (begin.failed()) {
logger.info("Failed initializing pool");
}
});
當數據庫運行時,一切都按預期進行,但是當數據庫處於離線狀態時,我沒有收到任何通知並且 Verticles 繼續運行。
我有以下 function:
public static void createTable(final String table) {
pool.query("CREATE TABLE IF NOT EXISTS " + table + "(Id BIGINT PRIMARY KEY, Lat double(8,5), Lon double(8,5));")
.execute(res -> {
if (res.succeeded()) {
logger.info("Created table " + table);
} else {
logger.error("Failed creating table: " + res.cause().getMessage());
}
});
}
這在池初始化后立即調用。 45 秒后記錄器 output 打印連接超時。 (為什么connectOptions
中指定的時間甚至是 45 秒和 10 毫秒?)
所以我的問題是:是否可以檢查池是否成功初始化並對結果做出反應?
您似乎出於錯誤的原因使用了connectHandler
。 應該調用此方法來設置連接后掛鈎,而不是驗證數據庫是否已啟動。
當您創建池時,在您嘗試進行查詢之前什么也不會發生。 所以在創建池之后,立即調用createTable
:
MySQLConnectOptions connectOptions = new MySQLConnectOptions()
.setHost(hostname)
.setPort(port)
.setDatabase(dbname)
.setUser(user)
.setPassword(password)
.setCachePreparedStatements(true)
.setPreparedStatementCacheMaxSize(250)
.setPreparedStatementCacheSqlLimit(2048)
.setReconnectAttempts(1)
.setReconnectInterval(10);
PoolOptions poolOptions = new PoolOptions().setMaxSize(5);
pool = MySQLPool.pool(vertx, connectOptions, poolOptions);
createTable("my_table");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.