Example usage for io.vertx.sqlclient Transaction commit

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

Introduction

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

Prototype

void commit(Handler<AsyncResult<Void>> handler);

Source Link

Document

Like #commit with an handler to be notified when the transaction commit has completed

Usage

From source file:examples.SqlClientExamples.java

License:Apache License

public void transaction01(Pool pool) {
    pool.getConnection(res -> {//from w  ww . jav  a2 s.  com
        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());
                                }
                                // 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();
                }
            });
        }
    });
}

From source file:examples.SqlClientExamples.java

License:Apache License

public void transaction03(Pool pool) {

    // Acquire a transaction and begin the transaction
    pool.begin(res -> {//from   ww  w  . j a v  a 2 s.  c o m
        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
                }
            });
        }
    });
}