Example usage for io.vertx.mysqlclient MySQLPool getConnection

List of usage examples for io.vertx.mysqlclient MySQLPool getConnection

Introduction

In this page you can find the example usage for io.vertx.mysqlclient MySQLPool getConnection.

Prototype

void getConnection(Handler<AsyncResult<SqlConnection>> handler);

Source Link

Document

Get a connection from the pool.

Usage

From source file:examples.MySQLClientExamples.java

public void configureFromDataObject(Vertx vertx) {

    // Data object
    MySQLConnectOptions connectOptions = new MySQLConnectOptions().setPort(3306).setHost("the-host")
            .setDatabase("the-db").setUser("user").setPassword("secret");

    // Pool Options
    PoolOptions poolOptions = new PoolOptions().setMaxSize(5);

    // Create the pool from the data object
    MySQLPool pool = MySQLPool.pool(vertx, connectOptions, poolOptions);

    pool.getConnection(ar -> {
        // Handling your connection
    });/*www  .ja v a 2  s. c  o m*/
}

From source file:examples.MySQLClientExamples.java

public void connecting04(Vertx vertx) {

    // Connect options
    MySQLConnectOptions connectOptions = new MySQLConnectOptions().setPort(3306).setHost("the-host")
            .setDatabase("the-db").setUser("user").setPassword("secret");

    // Pool options
    PoolOptions poolOptions = new PoolOptions().setMaxSize(5);

    // Create the pooled client
    MySQLPool client = MySQLPool.pool(vertx, connectOptions, poolOptions);

    // Get a connection from the pool
    client.getConnection(ar1 -> {

        if (ar1.succeeded()) {

            System.out.println("Connected");

            // Obtain our connection
            SqlConnection conn = ar1.result();

            // All operations execute on the same connection
            conn.query("SELECT * FROM users WHERE id='julien'", ar2 -> {
                if (ar2.succeeded()) {
                    conn.query("SELECT * FROM users WHERE id='emad'", ar3 -> {
                        // Release the connection to the pool
                        conn.close();/*from  w  ww.  jav a  2  s . c o  m*/
                    });
                } else {
                    // Release the connection to the pool
                    conn.close();
                }
            });
        } else {
            System.out.println("Could not connect: " + ar1.cause().getMessage());
        }
    });
}