Search in sources :

Example 1 with PgConnectOptions

use of io.vertx.pgclient.PgConnectOptions 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 PgConnectOptions

use of io.vertx.pgclient.PgConnectOptions 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 PgConnectOptions

use of io.vertx.pgclient.PgConnectOptions in project raml-module-builder by folio-org.

the class PostgresClient method createPgConnectOptions.

static PgConnectOptions createPgConnectOptions(JsonObject sqlConfig) {
    PgConnectOptions pgConnectOptions = new PgConnectOptions();
    String host = sqlConfig.getString(HOST);
    if (host != null) {
        pgConnectOptions.setHost(host);
    }
    Integer port = sqlConfig.getInteger(PORT);
    if (port != null) {
        pgConnectOptions.setPort(port);
    }
    String username = sqlConfig.getString(USERNAME);
    if (username != null) {
        pgConnectOptions.setUser(username);
    }
    String password = sqlConfig.getString(PASSWORD);
    if (password != null) {
        pgConnectOptions.setPassword(password);
    }
    String database = sqlConfig.getString(DATABASE);
    if (database != null) {
        pgConnectOptions.setDatabase(database);
    }
    String serverPem = sqlConfig.getString(SERVER_PEM);
    if (serverPem != null) {
        pgConnectOptions.setSslMode(SslMode.VERIFY_FULL);
        pgConnectOptions.setHostnameVerificationAlgorithm("HTTPS");
        pgConnectOptions.setPemTrustOptions(new PemTrustOptions().addCertValue(Buffer.buffer(serverPem)));
        pgConnectOptions.setEnabledSecureTransportProtocols(Collections.singleton("TLSv1.3"));
        if (OpenSSLEngineOptions.isAvailable()) {
            pgConnectOptions.setOpenSslEngineOptions(new OpenSSLEngineOptions());
        } else {
            pgConnectOptions.setJdkSslEngineOptions(new JdkSSLEngineOptions());
            log.error("Cannot run OpenSSL, using slow JDKSSL. Is netty-tcnative-boringssl-static for windows-x86_64, " + "osx-x86_64 or linux-x86_64 installed? https://netty.io/wiki/forked-tomcat-native.html " + "Is libc6-compat installed (if required)? https://github.com/pires/netty-tcnative-alpine");
        }
        log.debug("Enforcing SSL encryption for PostgreSQL connections, " + "requiring TLSv1.3 with server name certificate, " + "using " + (OpenSSLEngineOptions.isAvailable() ? "OpenSSL " + OpenSsl.versionString() : "JDKSSL"));
    }
    return pgConnectOptions;
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PgConnectOptions(io.vertx.pgclient.PgConnectOptions) JdkSSLEngineOptions(io.vertx.core.net.JdkSSLEngineOptions) PemTrustOptions(io.vertx.core.net.PemTrustOptions) OpenSSLEngineOptions(io.vertx.core.net.OpenSSLEngineOptions)

Example 4 with PgConnectOptions

use of io.vertx.pgclient.PgConnectOptions 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 5 with PgConnectOptions

use of io.vertx.pgclient.PgConnectOptions 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)

Aggregations

PgConnectOptions (io.vertx.pgclient.PgConnectOptions)8 PoolOptions (io.vertx.sqlclient.PoolOptions)5 PgPool (io.vertx.pgclient.PgPool)4 Pool (io.vertx.sqlclient.Pool)4 Row (io.vertx.sqlclient.Row)4 JsonObject (io.vertx.core.json.JsonObject)2 SqlConnection (io.vertx.sqlclient.SqlConnection)2 Transaction (io.vertx.sqlclient.Transaction)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 AESTest (org.folio.rest.security.AESTest)2 Test (org.junit.Test)2 JdkSSLEngineOptions (io.vertx.core.net.JdkSSLEngineOptions)1 OpenSSLEngineOptions (io.vertx.core.net.OpenSSLEngineOptions)1 PemTrustOptions (io.vertx.core.net.PemTrustOptions)1