io.vertx.pgclient.PgConnectOptions Java Examples

The following examples show how to use io.vertx.pgclient.PgConnectOptions. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PostgresClientTest.java    From raml-module-builder with Apache License 2.0 6 votes vote down vote up
@Test
public void testPgConnectOptionsFull() {
  JsonObject conf = new JsonObject()
      .put("host", "myhost")
      .put("port", 5433)
      .put("username", "myuser")
      .put("password", "mypassword")
      .put("database", "mydatabase")
      .put("connectionReleaseDelay", 1000);

  PgConnectOptions options = PostgresClient.createPgConnectOptions(conf);
  assertThat("myhost", is(options.getHost()));
  assertThat(5433, is(options.getPort()));
  assertThat("myuser", is(options.getUser()));
  assertThat("mypassword", is(options.getPassword()));
  assertThat("mydatabase", is(options.getDatabase()));
  assertThat(1000, is(options.getIdleTimeout()));
  assertThat(TimeUnit.MILLISECONDS, is(options.getIdleTimeoutUnit()));
}
 
Example #2
Source File: ContainerPgRule.java    From vertx-sql-client with Apache License 2.0 6 votes vote down vote up
@Override
protected void before() throws Throwable {
  // use an external database for testing
  if (isTestingWithExternalDatabase()) {
    
    if (ssl) {
      options =  PgConnectOptions.fromUri(tlsConnectionUri);
    }
    else {
      options =  PgConnectOptions.fromUri(connectionUri);        
    }
    
    return;
  }

  // We do not need to launch another server if it's a shared instance
  if (this.server != null) {
    return;
  }

  this.databaseVersion =  getPostgresVersion();
  options = startServer(databaseVersion);
}
 
Example #3
Source File: PgConnectionFactory.java    From vertx-sql-client with Apache License 2.0 6 votes vote down vote up
PgConnectionFactory(VertxInternal vertx, ContextInternal context, PgConnectOptions options) {

    NetClientOptions netClientOptions = new NetClientOptions(options);

    // Make sure ssl=false as we will use STARTLS
    netClientOptions.setSsl(false);

    this.context = context;
    this.sslMode = options.getSslMode();
    this.socketAddress = options.getSocketAddress();
    this.hostnameVerificationAlgorithm = netClientOptions.getHostnameVerificationAlgorithm();
    this.trustOptions = netClientOptions.getTrustOptions();
    this.database = options.getDatabase();
    this.username = options.getUser();
    this.password = options.getPassword();
    this.properties = new HashMap<>(options.getProperties());
    this.cachePreparedStatements = options.getCachePreparedStatements();
    this.pipeliningLimit = options.getPipeliningLimit();
    this.preparedStatementCacheSize = options.getPreparedStatementCacheMaxSize();
    this.preparedStatementCacheSqlFilter = options.getPreparedStatementCacheSqlFilter();
    this.client = vertx.createNetClient(netClientOptions);
  }
 
Example #4
Source File: PgTestBase.java    From okapi with Apache License 2.0 5 votes vote down vote up
public static PgConnectOptions getPgConnectOptions() {
  return new PgConnectOptions()
      .setPort(POSTGRESQL_CONTAINER.getFirstMappedPort())
      .setHost(POSTGRESQL_CONTAINER.getHost())
      .setDatabase(POSTGRESQL_CONTAINER.getDatabaseName())
      .setUser(POSTGRESQL_CONTAINER.getUsername())
      .setPassword(POSTGRESQL_CONTAINER.getPassword());
}
 
Example #5
Source File: PgTemplateTestBase.java    From vertx-sql-client with Apache License 2.0 5 votes vote down vote up
public static PgConnectOptions connectOptions() {
  Integer port = server.getMappedPort(PostgreSQLContainer.POSTGRESQL_PORT);
  String ip = server.getContainerIpAddress();
  return new PgConnectOptions()
    .setPort(port)
    .setHost(ip)
    .setDatabase("postgres")
    .setUser("postgres")
    .setPassword("postgres");
}
 
Example #6
Source File: EnvTest.java    From vertx-sql-client with Apache License 2.0 5 votes vote down vote up
@Test
public void testFoo() {
  PgConnectOptions options = PgConnectOptions.fromEnv();
  assertEquals("test_host", options.getHost());
  assertEquals("test_database", options.getDatabase());
  assertEquals("test_user", options.getUser());
  assertEquals("test_password", options.getPassword());
  assertEquals(SslMode.REQUIRE, options.getSslMode());
}
 
Example #7
Source File: PostgresClient.java    From raml-module-builder with Apache License 2.0 5 votes vote down vote up
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);
  }
  Integer connectionReleaseDelay = sqlConfig.getInteger(CONNECTION_RELEASE_DELAY, DEFAULT_CONNECTION_RELEASE_DELAY);
  pgConnectOptions.setIdleTimeout(connectionReleaseDelay);
  pgConnectOptions.setIdleTimeoutUnit(TimeUnit.MILLISECONDS);
  return pgConnectOptions;
}
 
Example #8
Source File: PostgresClient.java    From raml-module-builder with Apache License 2.0 5 votes vote down vote up
static PgPool createPgPool(Vertx vertx, JsonObject configuration) {
  PgConnectOptions connectOptions = createPgConnectOptions(configuration);

  PoolOptions poolOptions = new PoolOptions();
  poolOptions.setMaxSize(configuration.getInteger(MAX_POOL_SIZE, 4));

  return PgPool.pool(vertx, connectOptions, poolOptions);
}
 
Example #9
Source File: PostgresClientTest.java    From raml-module-builder with Apache License 2.0 5 votes vote down vote up
@Test
public void testPgConnectOptionsEmpty() {
  JsonObject conf = new JsonObject();
  PgConnectOptions options = PostgresClient.createPgConnectOptions(conf);
  assertThat("localhost", is(options.getHost()));
  assertThat(5432, is(options.getPort()));
  assertThat("user", is(options.getUser()));
  assertThat("pass", is(options.getPassword()));
  assertThat("db", is(options.getDatabase()));
  assertThat(60000, is(options.getIdleTimeout()));
  assertThat(TimeUnit.MILLISECONDS, is(options.getIdleTimeoutUnit()));
}
 
Example #10
Source File: ContainerPgRule.java    From vertx-sql-client with Apache License 2.0 5 votes vote down vote up
public synchronized PgConnectOptions startServer(String databaseVersion) throws Exception {
  initServer(databaseVersion);
  server.start();

  return new PgConnectOptions()
      .setPort(server.getMappedPort(PostgreSQLContainer.POSTGRESQL_PORT))
      .setHost(server.getContainerIpAddress())
      .setDatabase("postgres")
      .setUser("postgres")
      .setPassword("postgres");
}
 
Example #11
Source File: ReactiveDatabaseClientProvider.java    From vertx-jooq with MIT License 5 votes vote down vote up
private PgConnectOptions getOptions() {
    return new PgConnectOptions().setHost("127.0.0.1")
            .setPort(5432)
            .setUser(Credentials.POSTGRES.getUser())
            .setDatabase("postgres")
            .setPassword(Credentials.POSTGRES.getPassword())
            ;
}
 
Example #12
Source File: PgConnectionImpl.java    From vertx-sql-client with Apache License 2.0 5 votes vote down vote up
public static Future<PgConnection> connect(ContextInternal context, PgConnectOptions options) {
  if (options.isUsingDomainSocket() && !context.owner().isNativeTransportEnabled()) {
    return context.failedFuture("Native transport is not available");
  } else {
    PgConnectionFactory client = new PgConnectionFactory(context.owner(), context, options);
    return client.connect()
      .map(conn -> {
      QueryTracer tracer = context.tracer() == null ? null : new QueryTracer(context.tracer(), options);
      PgConnectionImpl pgConn = new PgConnectionImpl(client, context, conn, tracer, null);
      conn.init(pgConn);
      return pgConn;
    });
  }
}
 
Example #13
Source File: PgDriver.java    From vertx-sql-client with Apache License 2.0 5 votes vote down vote up
private static PgConnectOptions wrap(SqlConnectOptions options) {
  if (options instanceof PgConnectOptions) {
    return (PgConnectOptions) options; 
  } else {
    return new PgConnectOptions(options);
  }
}
 
Example #14
Source File: DBRest.java    From FrameworkBenchmarks with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public DBRest(GreenRuntime runtime, PgConnectOptions options, PoolOptions poolOptions, int pipelineBits, 
		      int maxResponseCount, int maxResponseSize) {
	
	pm = new PoolManager(options, poolOptions);
		
	HTTPResponseService service = runtime.newCommandChannel().newHTTPResponseService(
			                maxResponseCount, 
			                maxResponseSize);
	
	processUpdate = new ProcessUpdate(pipelineBits, service, pm);		
	processFortune = new ProcessFortune(pipelineBits, service, pm);
	processQuery = new ProcessQuery(pipelineBits, service, pm);
	
}
 
Example #15
Source File: PoolManager.java    From FrameworkBenchmarks with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public PoolManager(PgConnectOptions options, PoolOptions poolOptions) {
	this.options = options;
	this.poolOptions = poolOptions;
	
	this.vertx = Vertx.vertx(new VertxOptions()
			  .setPreferNativeTransport(true)
			  .setWorkerPoolSize(4)//limit threads for this track
			);
	
	boolean usingNative = vertx.isNativeTransportEnabled();
	System.out.println("Running with native: " + usingNative);
}
 
Example #16
Source File: PgPoolRecorder.java    From quarkus with Apache License 2.0 5 votes vote down vote up
private PgPool legacyInitialize(Vertx vertx, DataSourceRuntimeConfig dataSourceRuntimeConfig,
        LegacyDataSourceRuntimeConfig legacyDataSourceRuntimeConfig,
        LegacyDataSourceReactivePostgreSQLConfig legacyDataSourceReactivePostgreSQLConfig) {
    PoolOptions poolOptions = legacyToPoolOptionsLegacy(legacyDataSourceRuntimeConfig);
    PgConnectOptions pgConnectOptions = legacyToPostgreSQLConnectOptions(dataSourceRuntimeConfig,
            legacyDataSourceRuntimeConfig, legacyDataSourceReactivePostgreSQLConfig);
    return PgPool.pool(vertx, pgConnectOptions, poolOptions);
}
 
Example #17
Source File: PgPoolRecorder.java    From quarkus with Apache License 2.0 5 votes vote down vote up
private PgPool initialize(Vertx vertx, DataSourceRuntimeConfig dataSourceRuntimeConfig,
        DataSourceReactiveRuntimeConfig dataSourceReactiveRuntimeConfig,
        DataSourceReactivePostgreSQLConfig dataSourceReactivePostgreSQLConfig) {
    PoolOptions poolOptions = toPoolOptions(dataSourceRuntimeConfig, dataSourceReactiveRuntimeConfig,
            dataSourceReactivePostgreSQLConfig);
    PgConnectOptions pgConnectOptions = toPgConnectOptions(dataSourceRuntimeConfig, dataSourceReactiveRuntimeConfig,
            dataSourceReactivePostgreSQLConfig);
    if (dataSourceReactiveRuntimeConfig.threadLocal.isPresent() &&
            dataSourceReactiveRuntimeConfig.threadLocal.get()) {
        return new ThreadLocalPgPool(vertx, pgConnectOptions, poolOptions);
    }
    return PgPool.pool(vertx, pgConnectOptions, poolOptions);
}
 
Example #18
Source File: App.java    From FrameworkBenchmarks with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public PgClientBenchmark(Vertx vertx, JsonObject config) {
  PgConnectOptions options = new PgConnectOptions()
    .setCachePreparedStatements(true)
    .setHost(config.getString("host"))
    .setPort(config.getInteger("port", 5432))
    .setUser(config.getString("username"))
    .setPassword(config.getString("password"))
    .setDatabase(config.getString("database"));

  client = PgPool.pool(vertx, options, new PoolOptions().setMaxSize(4));
  this.engine = RockerTemplateEngine.create();
}
 
Example #19
Source File: IntegrationTest.java    From vertx-in-action with MIT License 5 votes vote down vote up
@BeforeAll
void prepareSpec(Vertx vertx, VertxTestContext testContext) {
  requestSpecification = new RequestSpecBuilder()
    .addFilters(asList(new ResponseLoggingFilter(), new RequestLoggingFilter()))
    .setBaseUri("http://localhost:4000/")
    .setBasePath("/api/v1")
    .build();

  String insertQuery = "INSERT INTO stepevent VALUES($1, $2, $3::timestamp, $4)";
  List<Tuple> data = Arrays.asList(
    Tuple.of("a1b2c3", 1, LocalDateTime.of(2019, 6, 16, 10, 3), 250),
    Tuple.of("a1b2c3", 2, LocalDateTime.of(2019, 6, 16, 12, 30), 1000),
    Tuple.of("a1b2c3", 3, LocalDateTime.of(2019, 6, 15, 23, 00), 5005)
  );
  PgPool pgPool = PgPool.pool(vertx, new PgConnectOptions()
    .setHost("localhost")
    .setDatabase("postgres")
    .setUser("postgres")
    .setPassword("vertx-in-action"), new PoolOptions());

  pgPool.preparedQuery(insertQuery)
    .rxExecuteBatch(data)
    .ignoreElement()
    .andThen(vertx.rxDeployVerticle(new PublicApiVerticle()))
    .ignoreElement()
    .andThen(vertx.rxDeployVerticle("tenksteps.userprofiles.UserProfileApiVerticle"))
    .ignoreElement()
    .andThen(vertx.rxDeployVerticle("tenksteps.activities.ActivityApiVerticle"))
    .ignoreElement()
    .andThen(Completable.fromAction(pgPool::close))
    .subscribe(testContext::completeNow, testContext::failNow);
}
 
Example #20
Source File: PgConfig.java    From vertx-in-action with MIT License 5 votes vote down vote up
public static PgConnectOptions pgConnectOpts() {
  return new PgConnectOptions()
    .setHost("localhost")
    .setDatabase("postgres")
    .setUser("postgres")
    .setPassword("vertx-in-action");
}
 
Example #21
Source File: PostgresAutoConfiguration.java    From vertx-spring-boot with Apache License 2.0 5 votes vote down vote up
@Bean
public SimpleReactivePostgresTemplate reactivePostgresTemplate(Vertx vertx) {
    PgConnectOptions connectOptions = new PgConnectOptions()
        .setUser("sa")
        .setPassword("sa")
        .setDatabase("sa");
    PoolOptions poolOptions = new PoolOptions();
    PgPool pgPool = PgPool.pool(new io.vertx.axle.core.Vertx(vertx), connectOptions, poolOptions);

    BasicRelationalConverter converter = new BasicRelationalConverter(new RelationalMappingContext());
    ColumnEntityWriter entityWriter = new ColumnEntityWriter(converter);

    return new SimpleReactivePostgresTemplate(pgPool, entityWriter, converter);
}
 
Example #22
Source File: PgRule.java    From vertx-sql-client with Apache License 2.0 4 votes vote down vote up
public PgConnectOptions options() {
  return new PgConnectOptions(options);
}
 
Example #23
Source File: PostgresHandle.java    From okapi with Apache License 2.0 4 votes vote down vote up
PgConnectOptions getOptions() {
  return connectOptions;
}
 
Example #24
Source File: PostgresHandle.java    From okapi with Apache License 2.0 4 votes vote down vote up
PostgresHandle(Vertx vertx, JsonObject conf) {
  String val;

  connectOptions = new PgConnectOptions();
  val = Config.getSysConf("postgres_host", null, conf);
  if (val != null) {
    connectOptions.setHost(val);
  }
  val = Config.getSysConf("postgres_port", null, conf);
  Logger logger = OkapiLogger.get();
  if (val != null) {
    try {
      connectOptions.setPort(Integer.parseInt(val));
    } catch (NumberFormatException e) {
      logger.warn("Bad postgres_port value: {}: {}", val, e.getMessage());
    }
  }

  // postgres_user is supported for system configuration (-D option) only and is deprecated
  connectOptions.setUser(Config.getSysConf("postgres_username",
      Config.getSysConf("postgres_user", "okapi", new JsonObject()), conf));
  connectOptions.setPassword(Config.getSysConf("postgres_password", "okapi25", conf));
  connectOptions.setDatabase(Config.getSysConf("postgres_database", "okapi", conf));
  String serverPem = Config.getSysConf("postgres_server_pem", null, conf);
  if (serverPem != null) {
    logger.debug("Enforcing SSL encryption for PostgreSQL connections, "
        + "requiring TLSv1.3 with server name certificate");
    connectOptions.setSslMode(SslMode.VERIFY_FULL);
    connectOptions.setHostnameVerificationAlgorithm("HTTPS");
    connectOptions.setPemTrustOptions(
        new PemTrustOptions().addCertValue(Buffer.buffer(serverPem)));
    connectOptions.setEnabledSecureTransportProtocols(Collections.singleton("TLSv1.3"));
    connectOptions.setOpenSslEngineOptions(new OpenSSLEngineOptions());
  }

  PoolOptions poolOptions = new PoolOptions();
  poolOptions.setMaxSize(5);

  pool = PgPool.pool(vertx, connectOptions, poolOptions);
  logger.debug("created");
}
 
Example #25
Source File: PgTransactionTest.java    From vertx-sql-client with Apache License 2.0 4 votes vote down vote up
@Override
protected Pool nonTxPool() {
  return PgPool.pool(vertx, new PgConnectOptions(rule.options()), new PoolOptions().setMaxSize(1));
}
 
Example #26
Source File: PgTransactionTest.java    From vertx-sql-client with Apache License 2.0 4 votes vote down vote up
@Override
protected Pool createPool() {
  return PgPool.pool(vertx, new PgConnectOptions(rule.options()), new PoolOptions().setMaxSize(1));
}
 
Example #27
Source File: PgRule.java    From vertx-sql-client with Apache License 2.0 4 votes vote down vote up
public synchronized static PgConnectOptions startPg() throws Exception {
  return startPg(false);
}
 
Example #28
Source File: ContainerPgRule.java    From vertx-sql-client with Apache License 2.0 4 votes vote down vote up
public PgConnectOptions options() {
  return new PgConnectOptions(options);
}
 
Example #29
Source File: PgDriver.java    From vertx-sql-client with Apache License 2.0 4 votes vote down vote up
@Override
public boolean acceptsOptions(SqlConnectOptions options) {
  return options instanceof PgConnectOptions || SqlConnectOptions.class.equals(options.getClass());
}
 
Example #30
Source File: ThreadLocalPgPool.java    From quarkus with Apache License 2.0 4 votes vote down vote up
public ThreadLocalPgPool(Vertx vertx, PgConnectOptions pgConnectOptions, PoolOptions poolOptions) {
    super(vertx, poolOptions);
    this.pgConnectOptions = pgConnectOptions;
}