Example usage for io.vertx.sqlclient Pool getConnection

List of usage examples for io.vertx.sqlclient Pool getConnection

Introduction

In this page you can find the example usage for io.vertx.sqlclient Pool getConnection.

Prototype

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

Source Link

Document

Get a connection from the pool.

Usage

From source file:examples.SqlClientExamples.java

License:Apache License

public void usingConnections01(Vertx vertx, Pool pool) {

    pool.getConnection(ar1 -> {
        if (ar1.succeeded()) {
            SqlConnection connection = ar1.result();

            connection.query("SELECT * FROM users WHERE id='julien'", ar2 -> {
                if (ar1.succeeded()) {
                    connection.query("SELECT * FROM users WHERE id='paulo'", ar3 -> {
                        // Do something with rows and return the connection to the pool
                        connection.close();
                    });/*from w w w.  j  a v  a  2s.  c o  m*/
                } else {
                    // Return the connection to the pool
                    connection.close();
                }
            });
        }
    });
}

From source file:examples.SqlClientExamples.java

License:Apache License

public void transaction01(Pool pool) {
    pool.getConnection(res -> {
        if (res.succeeded()) {

            // Transaction must use a connection
            SqlConnection conn = res.result();

            // Begin the transaction
            Transaction tx = conn.begin();

            // Various statements
            conn.query("INSERT INTO Users (first_name,last_name) VALUES ('Julien','Viet')", ar1 -> {
                if (ar1.succeeded()) {
                    conn.query("INSERT INTO Users (first_name,last_name) VALUES ('Emad','Alblueshi')", ar2 -> {
                        if (ar2.succeeded()) {
                            // Commit the transaction
                            tx.commit(ar3 -> {
                                if (ar3.succeeded()) {
                                    System.out.println("Transaction succeeded");
                                } else {
                                    System.out.println("Transaction failed " + ar3.cause().getMessage());
                                }//from w  w w. java2s.  c  o m
                                // Return the connection to the pool
                                conn.close();
                            });
                        } else {
                            // Return the connection to the pool
                            conn.close();
                        }
                    });
                } else {
                    // Return the connection to the pool
                    conn.close();
                }
            });
        }
    });
}