Example usage for io.vertx.sqlclient SqlConnection begin

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

Introduction

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

Prototype

Transaction begin();

Source Link

Document

Begin a transaction and returns a Transaction for controlling and tracking this transaction.

Usage

From source file:examples.SqlClientExamples.java

License:Apache License

public void transaction01(Pool pool) {
    pool.getConnection(res -> {/*  w  w  w  .  ja va2  s .  c o  m*/
        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 usingCursors01(SqlConnection connection) {
    connection.prepare("SELECT * FROM users WHERE first_name LIKE $1", ar1 -> {
        if (ar1.succeeded()) {
            PreparedQuery pq = ar1.result();

            // Cursors require to run within a transaction
            Transaction tx = connection.begin();

            // Create a cursor
            Cursor cursor = pq.cursor(Tuple.of("julien"));

            // Read 50 rows
            cursor.read(50, ar2 -> {/*ww  w  .ja v a2  s. c  o m*/
                if (ar2.succeeded()) {
                    RowSet<Row> rows = ar2.result();

                    // Check for more ?
                    if (cursor.hasMore()) {
                        // Repeat the process...
                    } else {
                        // No more rows - commit the transaction
                        tx.commit();
                    }
                }
            });
        }
    });
}

From source file:examples.SqlClientExamples.java

License:Apache License

public void usingCursors03(SqlConnection connection) {
    connection.prepare("SELECT * FROM users WHERE first_name LIKE $1", ar1 -> {
        if (ar1.succeeded()) {
            PreparedQuery pq = ar1.result();

            // Streams require to run within a transaction
            Transaction tx = connection.begin();

            // Fetch 50 rows at a time
            RowStream<Row> stream = pq.createStream(50, Tuple.of("julien"));

            // Use the stream
            stream.exceptionHandler(err -> {
                System.out.println("Error: " + err.getMessage());
            });/*from ww w  .j a v a2  s  . c o  m*/
            stream.endHandler(v -> {
                tx.commit();
                System.out.println("End of stream");
            });
            stream.handler(row -> {
                System.out.println("User: " + row.getString("last_name"));
            });
        }
    });
}