package com.be.edge.asset.source; import io.vertx.core.AbstractVerticle; import io.vertx.core.Promise; import io.vertx.core.json.JsonObject; import io.vertx.mysqlclient.MySQLConnectOptions; import io.vertx.mysqlclient.MySQLPool; import io.vertx.sqlclient.PoolOptions; import io.vertx.sqlclient.Row; import io.vertx.sqlclient.RowSet; import lombok.extern.slf4j.Slf4j; @Slf4j public class MySQLVerticle extends AbstractVerticle { private MySQLPool client; @Override public void start(Promise<Void> startPromise) throws Exception { initMySQLPool(); /* client.query("SELECT * FROM data_list WHERE id = 1") .execute(ar -> { if (ar.succeeded()) { RowSet<Row> result = ar.result(); log.info("Got {} rows {}", result.size(), result); } else { log.info("Failure {}", ar.cause().getMessage()); } client.close(); }); */ client.getConnection().compose(conn -> { // All operations execute on the same connection return conn .query("SELECT * FROM data_list WHERE id = 1") .execute() .compose(res -> conn .query("SELECT * FROM data_list WHERE id = 2") .execute()) .onComplete(ar -> { // Release the connection to the pool conn.close(); }); }).onComplete(ar -> { if (ar.succeeded()) { RowSet<Row> result = ar.result(); log.info("Got {} rows {}", result.rowCount(), result); for (Row row : result) { log.info("data {} {}", row.getInteger(0), row.getString(1)); } } else { System.out.println("Something went wrong " + ar.cause().getMessage()); } }); } private void initMySQLPool() { JsonObject config = config(); MySQLConnectOptions connectOptions = new MySQLConnectOptions(config.getJsonObject("connect")); PoolOptions poolOptions = new PoolOptions(config.getJsonObject("pool")); client = MySQLPool.pool(vertx, connectOptions, poolOptions); } }