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();
});
});
});
});
}
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);
}
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;
}
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();
});
});
});
});
}
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);
}
});
});
});
});
});
}
Aggregations