Example usage for io.vertx.sqlclient SqlConnection close

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

Introduction

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

Prototype

void close();

Source Link

Document

Close the current connection after all the pending commands have been processed.

Usage

From source file:examples.MySQLClientExamples.java

public void connecting04(Vertx vertx) {

    // Connect options
    MySQLConnectOptions connectOptions = new MySQLConnectOptions().setPort(3306).setHost("the-host")
            .setDatabase("the-db").setUser("user").setPassword("secret");

    // Pool options
    PoolOptions poolOptions = new PoolOptions().setMaxSize(5);

    // Create the pooled client
    MySQLPool client = MySQLPool.pool(vertx, connectOptions, poolOptions);

    // Get a connection from the pool
    client.getConnection(ar1 -> {/*w  ww. java 2 s.  com*/

        if (ar1.succeeded()) {

            System.out.println("Connected");

            // Obtain our connection
            SqlConnection conn = ar1.result();

            // All operations execute on the same connection
            conn.query("SELECT * FROM users WHERE id='julien'", ar2 -> {
                if (ar2.succeeded()) {
                    conn.query("SELECT * FROM users WHERE id='emad'", ar3 -> {
                        // Release the connection to the pool
                        conn.close();
                    });
                } else {
                    // Release the connection to the pool
                    conn.close();
                }
            });
        } else {
            System.out.println("Could not connect: " + ar1.cause().getMessage());
        }
    });
}

From source file:examples.PgClientExamples.java

License:Apache License

public void connecting04(Vertx vertx) {

    // Connect options
    PgConnectOptions connectOptions = new PgConnectOptions().setPort(5432).setHost("the-host")
            .setDatabase("the-db").setUser("user").setPassword("secret");

    // Pool options
    PoolOptions poolOptions = new PoolOptions().setMaxSize(5);

    // Create the pooled client
    PgPool client = PgPool.pool(vertx, connectOptions, poolOptions);

    // Get a connection from the pool
    client.getConnection(ar1 -> {/*ww w  . j  av a2s.  c om*/

        if (ar1.succeeded()) {

            System.out.println("Connected");

            // Obtain our connection
            SqlConnection conn = ar1.result();

            // All operations execute on the same connection
            conn.query("SELECT * FROM users WHERE id='julien'", ar2 -> {
                if (ar2.succeeded()) {
                    conn.query("SELECT * FROM users WHERE id='emad'", ar3 -> {
                        // Release the connection to the pool
                        conn.close();
                    });
                } else {
                    // Release the connection to the pool
                    conn.close();
                }
            });
        } else {
            System.out.println("Could not connect: " + ar1.cause().getMessage());
        }
    });
}

From source file:examples.SqlClientExamples.java

License:Apache License

public void usingConnections01(Vertx vertx, Pool pool) {

    pool.getConnection(ar1 -> {/*www. j a v  a 2 s.com*/
        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();
                    });
                } 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 -> {/*from   w ww .  ja v  a 2  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();
                }
            });
        }
    });
}