简体   繁体   中英

Check if Vert.x MySQL Pool initialized

I am pretty new to Vert.x and I want to use its MySQL Pool.

This is my setup:

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

When the database is running, everything works as expected, but when the database is offline, I don't get any notification and the verticles keep running.

I have the following 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());
                    }
                });
    }

This is called immediately after the pools initialization. The logger output prints 45 seconds later, that the connection timed out. (why is it even 45 seconds and noch 10ms as specified in the connectOptions ?)

So my question is: Is it possible to check if the pool initialized successfully and react to the outcome?

It seems you use connectHandler for a wrong reason. This method should be invoked to set up a post-connection hook, not to verify if the database is up.

When you create the pool, nothing happens until you try to make a query. So after the pool creation, invoke the createTable immediately:

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");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM