Example usage for io.vertx.sqlclient Transaction query

List of usage examples for io.vertx.sqlclient Transaction query

Introduction

In this page you can find the example usage for io.vertx.sqlclient Transaction query.

Prototype

@Override
    Transaction query(String sql, Handler<AsyncResult<RowSet<Row>>> handler);

Source Link

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 -> {/*w ww.ja  v a  2 s .  c om*/
        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());
                                }
                            });
                        }
                    });
                } else {
                    // No need to close connection as transaction will abort and be returned to the pool
                }
            });
        }
    });
}