Example usage for io.vertx.sqlclient Pool begin

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

Introduction

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

Prototype

void begin(Handler<AsyncResult<Transaction>> handler);

Source Link

Document

Borrow a connection from the pool and begin a transaction, the underlying connection will be returned to the pool when the transaction ends.

Usage

From source file:examples.SqlClientExamples.java

License:Apache License

public void transaction03(Pool pool) {

    // Acquire a transaction and begin the transaction
    pool.begin(res -> {
        if (res.succeeded()) {

            // Get the transaction
            Transaction tx = res.result();

            // Various statements
            tx.query("INSERT INTO Users (first_name,last_name) VALUES ('Julien','Viet')", ar1 -> {
                if (ar1.succeeded()) {
                    tx.query("INSERT INTO Users (first_name,last_name) VALUES ('Emad','Alblueshi')", ar2 -> {
                        if (ar2.succeeded()) {
                            // Commit the transaction
                            // the connection will automatically return to the pool
                            tx.commit(ar3 -> {
                                if (ar3.succeeded()) {
                                    System.out.println("Transaction succeeded");
                                } else {
                                    System.out.println("Transaction failed " + ar3.cause().getMessage());
                                }/* w ww.  j av a 2  s.  c om*/
                            });
                        }
                    });
                } else {
                    // No need to close connection as transaction will abort and be returned to the pool
                }
            });
        }
    });
}