Search in sources :

Example 1 with PoolOptions

use of io.vertx.sqlclient.PoolOptions in project vertx-examples by vert-x3.

the class SqlClientExample method start.

@Override
public void start() {
    Pool pool = PgPool.pool(vertx, new PgConnectOptions().setPort(5432).setHost("the-host").setDatabase("the-db").setUser("user").setPassword("secret"), new PoolOptions().setMaxSize(4));
    // Uncomment for MySQL
    // Pool pool = MySQLPool.pool(vertx, new MySQLConnectOptions()
    // .setPort(5432)
    // .setHost("the-host")
    // .setDatabase("the-db")
    // .setUser("user")
    // .setPassword("secret"), new PoolOptions().setMaxSize(4));
    pool.getConnection(res1 -> {
        if (res1.failed()) {
            System.err.println(res1.cause().getMessage());
            return;
        }
        SqlConnection connection = res1.result();
        // create a test table
        connection.query("create table test(id int primary key, name varchar(255))").execute(res2 -> {
            if (res2.failed()) {
                connection.close();
                System.err.println("Cannot create the table");
                res2.cause().printStackTrace();
                return;
            }
            // insert some test data
            connection.query("insert into test values (1, 'Hello'), (2, 'World')").execute(res3 -> {
                // query some data with arguments
                connection.preparedQuery("select * from test where id = ?").execute(Tuple.of(2), rs -> {
                    if (rs.failed()) {
                        System.err.println("Cannot retrieve the data from the database");
                        rs.cause().printStackTrace();
                        return;
                    }
                    for (Row line : rs.result()) {
                        System.out.println("" + line);
                    }
                    // and close the connection
                    connection.close();
                });
            });
        });
    });
}
Also used : PgConnectOptions(io.vertx.pgclient.PgConnectOptions) PoolOptions(io.vertx.sqlclient.PoolOptions) PgPool(io.vertx.pgclient.PgPool) Pool(io.vertx.sqlclient.Pool) SqlConnection(io.vertx.sqlclient.SqlConnection) Row(io.vertx.sqlclient.Row)

Example 2 with PoolOptions

use of io.vertx.sqlclient.PoolOptions in project raml-module-builder by folio-org.

the class PostgresClient method createPgPool.

static PgPool createPgPool(Vertx vertx, JsonObject configuration) {
    PgConnectOptions connectOptions = createPgConnectOptions(configuration);
    PoolOptions poolOptions = new PoolOptions();
    poolOptions.setMaxSize(configuration.getInteger(MAX_SHARED_POOL_SIZE, configuration.getInteger(MAX_POOL_SIZE, 4)));
    Integer connectionReleaseDelay = configuration.getInteger(CONNECTION_RELEASE_DELAY, DEFAULT_CONNECTION_RELEASE_DELAY);
    poolOptions.setIdleTimeout(connectionReleaseDelay);
    poolOptions.setIdleTimeoutUnit(TimeUnit.MILLISECONDS);
    return PgPool.pool(vertx, connectOptions, poolOptions);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PgConnectOptions(io.vertx.pgclient.PgConnectOptions) PoolOptions(io.vertx.sqlclient.PoolOptions)

Example 3 with PoolOptions

use of io.vertx.sqlclient.PoolOptions in project vertx-examples by vert-x3.

the class SqlClientExample method start.

@Override
public void start() throws Exception {
    Pool pool = PgPool.pool(vertx, new PgConnectOptions().setPort(5432).setHost("the-host").setDatabase("the-db").setUser("user").setPassword("secret"), new PoolOptions().setMaxSize(4));
    // Uncomment for MySQL
    // Pool pool = MySQLPool.pool(vertx, new MySQLConnectOptions()
    // .setPort(5432)
    // .setHost("the-host")
    // .setDatabase("the-db")
    // .setUser("user")
    // .setPassword("secret"), new PoolOptions().setMaxSize(4));
    pool.getConnection(res1 -> {
        if (res1.failed()) {
            System.err.println(res1.cause().getMessage());
            return;
        }
        SqlConnection connection = res1.result();
        // create a test table
        connection.query("create table test(id int primary key, name varchar(255))").execute(res2 -> {
            if (res2.failed()) {
                connection.close();
                System.err.println("Cannot create the table");
                res2.cause().printStackTrace();
                return;
            }
            // insert some test data
            connection.query("insert into test values (1, 'Hello'), (2, 'World')").execute(res3 -> {
                // query some data with arguments
                connection.query("select * from test").execute(rs -> {
                    if (rs.failed()) {
                        System.err.println("Cannot retrieve the data from the database");
                        rs.cause().printStackTrace();
                        return;
                    }
                    for (Row line : rs.result()) {
                        System.out.println("" + line);
                    }
                    // and close the connection
                    connection.close();
                });
            });
        });
    });
}
Also used : PgConnectOptions(io.vertx.pgclient.PgConnectOptions) PoolOptions(io.vertx.sqlclient.PoolOptions) PgPool(io.vertx.pgclient.PgPool) Pool(io.vertx.sqlclient.Pool) SqlConnection(io.vertx.sqlclient.SqlConnection) Row(io.vertx.sqlclient.Row)

Example 4 with PoolOptions

use of io.vertx.sqlclient.PoolOptions in project vertx-examples by vert-x3.

the class SqlClientExample method start.

@Override
public void start() {
    Pool pool = PgPool.pool(vertx, new PgConnectOptions().setPort(5432).setHost("the-host").setDatabase("the-db").setUser("user").setPassword("secret"), new PoolOptions().setMaxSize(4));
    // Uncomment for MySQL
    // Pool pool = MySQLPool.pool(vertx, new MySQLConnectOptions()
    // .setPort(5432)
    // .setHost("the-host")
    // .setDatabase("the-db")
    // .setUser("user")
    // .setPassword("secret"), new PoolOptions().setMaxSize(4));
    pool.begin(res1 -> {
        if (res1.failed()) {
            System.err.println(res1.cause().getMessage());
            return;
        }
        Transaction tx = res1.result();
        // create a test table
        tx.query("create table test(id int primary key, name varchar(255))").execute(res2 -> {
            if (res2.failed()) {
                tx.close();
                System.err.println("Cannot create the table");
                res2.cause().printStackTrace();
                return;
            }
            // insert some test data
            tx.query("insert into test values (1, 'Hello'), (2, 'World')").execute(res3 -> {
                // rollback transaction
                tx.rollback(res4 -> {
                    // query some data with arguments
                    pool.query("select * from test").execute(rs -> {
                        if (rs.failed()) {
                            System.err.println("Cannot retrieve the data from the database");
                            rs.cause().printStackTrace();
                            return;
                        }
                        for (Row line : rs.result()) {
                            System.out.println("" + line);
                        }
                    });
                });
            });
        });
    });
}
Also used : PgConnectOptions(io.vertx.pgclient.PgConnectOptions) Transaction(io.vertx.sqlclient.Transaction) PoolOptions(io.vertx.sqlclient.PoolOptions) PgPool(io.vertx.pgclient.PgPool) Pool(io.vertx.sqlclient.Pool) Row(io.vertx.sqlclient.Row)

Example 5 with PoolOptions

use of io.vertx.sqlclient.PoolOptions in project vertx-examples by vert-x3.

the class SqlClientExample method start.

@Override
public void start() {
    Pool pool = PgPool.pool(vertx, new PgConnectOptions().setPort(5432).setHost("the-host").setDatabase("the-db").setUser("user").setPassword("secret"), new PoolOptions().setMaxSize(4));
    // Uncomment for MySQL
    // Pool pool = MySQLPool.pool(vertx, new MySQLConnectOptions()
    // .setPort(5432)
    // .setHost("the-host")
    // .setDatabase("the-db")
    // .setUser("user")
    // .setPassword("secret"), new PoolOptions().setMaxSize(4));
    pool.begin(res1 -> {
        if (res1.failed()) {
            System.err.println(res1.cause().getMessage());
            return;
        }
        Transaction tx = res1.result();
        // create a test table
        tx.query("create table test(id int primary key, name varchar(255))").execute(res2 -> {
            if (res2.failed()) {
                tx.close();
                System.err.println("Cannot create the table");
                res2.cause().printStackTrace();
                return;
            }
            // insert some test data
            tx.query("insert into test values (1, 'Hello'), (2, 'World')").execute(res3 -> {
                // query some data with arguments
                tx.query("select * from test").execute(rs -> {
                    if (rs.failed()) {
                        System.err.println("Cannot retrieve the data from the database");
                        rs.cause().printStackTrace();
                        return;
                    }
                    for (Row line : rs.result()) {
                        System.out.println("" + line);
                    }
                    // and close the connection
                    tx.commit();
                });
            });
        });
    });
}
Also used : PgConnectOptions(io.vertx.pgclient.PgConnectOptions) Transaction(io.vertx.sqlclient.Transaction) PoolOptions(io.vertx.sqlclient.PoolOptions) PgPool(io.vertx.pgclient.PgPool) Pool(io.vertx.sqlclient.Pool) Row(io.vertx.sqlclient.Row)

Aggregations

PgConnectOptions (io.vertx.pgclient.PgConnectOptions)5 PoolOptions (io.vertx.sqlclient.PoolOptions)5 PgPool (io.vertx.pgclient.PgPool)4 Pool (io.vertx.sqlclient.Pool)4 Row (io.vertx.sqlclient.Row)4 SqlConnection (io.vertx.sqlclient.SqlConnection)2 Transaction (io.vertx.sqlclient.Transaction)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1