Example usage for io.vertx.pgclient PgPool getConnection

List of usage examples for io.vertx.pgclient PgPool getConnection

Introduction

In this page you can find the example usage for io.vertx.pgclient PgPool getConnection.

Prototype

void getConnection(Handler<AsyncResult<SqlConnection>> handler);

Source Link

Document

Get a connection from the pool.

Usage

From source file:examples.PgClientExamples.java

License:Apache License

public void configureFromDataObject(Vertx vertx) {

    // Data object
    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 pool from the data object
    PgPool pool = PgPool.pool(vertx, connectOptions, poolOptions);

    pool.getConnection(ar -> {
        // Handling your connection
    });/*  ww  w . j a  va 2 s . c  o m*/
}

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 -> {

        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();//from   w  w  w.jav a2  s  .  c o m
                    });
                } else {
                    // Release the connection to the pool
                    conn.close();
                }
            });
        } else {
            System.out.println("Could not connect: " + ar1.cause().getMessage());
        }
    });
}