Java Code Examples for io.vertx.pgclient.PgPool

The following examples show how to use io.vertx.pgclient.PgPool. These examples are extracted from open source projects. 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 Project: quarkus   Source File: PgPoolRecorder.java    License: Apache License 2.0 6 votes vote down vote up
public RuntimeValue<PgPool> configurePgPool(RuntimeValue<Vertx> vertx,
        DataSourcesRuntimeConfig dataSourcesRuntimeConfig,
        DataSourceReactiveRuntimeConfig dataSourceReactiveRuntimeConfig,
        DataSourceReactivePostgreSQLConfig dataSourceReactivePostgreSQLConfig,
        LegacyDataSourcesRuntimeConfig legacyDataSourcesRuntimeConfig,
        LegacyDataSourceReactivePostgreSQLConfig legacyDataSourceReactivePostgreSQLConfig,
        boolean isLegacy,
        ShutdownContext shutdown) {

    PgPool pgPool;
    if (!isLegacy) {
        pgPool = initialize(vertx.getValue(), dataSourcesRuntimeConfig.defaultDataSource,
                dataSourceReactiveRuntimeConfig,
                dataSourceReactivePostgreSQLConfig);
    } else {
        pgPool = legacyInitialize(vertx.getValue(), dataSourcesRuntimeConfig.defaultDataSource,
                legacyDataSourcesRuntimeConfig.defaultDataSource, legacyDataSourceReactivePostgreSQLConfig);
    }

    shutdown.addShutdownTask(pgPool::close);
    return new RuntimeValue<>(pgPool);
}
 
Example 2
Source Project: quarkus   Source File: PgPoolProducer.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Produces the Axle PostGreSQL Pool instance. The instance is created lazily.
 *
 * @return the Axle PostGreSQL pool instance
 * @deprecated The Axle API is deprecated and will be removed in the future, use {@link #mutinyPgPool()} instead.
 */
@Singleton
@Produces
@Deprecated
public io.vertx.axle.pgclient.PgPool axlePgPool() {
    LOGGER.warn(
            "`io.vertx.axle.pgclient.PgPool` is deprecated and will be removed in a future version - it is "
                    + "recommended to switch to `io.vertx.mutiny.pgclient.PgPool`");
    return io.vertx.axle.pgclient.PgPool.newInstance(pgPool);
}
 
Example 3
Source Project: quarkus   Source File: PgPoolProducer.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Produces the RX PostGreSQL Pool instance. The instance is created lazily.
 *
 * @return the RX PostGreSQL pool instance
 * @deprecated The RX API is deprecated and will be removed in the future, use {@link #mutinyPgPool()} instead.
 */
@Singleton
@Produces
@Deprecated
public io.vertx.reactivex.pgclient.PgPool rxPgPool() {
    LOGGER.warn(
            "`io.vertx.reactivex.pgclient.PgPool` is deprecated and will be removed in a future version - it is "
                    + "recommended to switch to `io.vertx.mutiny.pgclient.PgPool`");
    return io.vertx.reactivex.pgclient.PgPool.newInstance(pgPool);
}
 
Example 4
Source Project: quarkus   Source File: PgPoolRecorder.java    License: 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 5
Source Project: quarkus   Source File: PgPoolRecorder.java    License: 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 6
Source Project: raml-module-builder   Source File: PostgresClient.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Close the SQL client of this PostgresClient instance.
 * @param whenDone invoked with the close result; additional close invocations
 *                 are always successful.
 */
public void closeClient(Handler<AsyncResult<Void>> whenDone) {
  if (client == null) {
    whenDone.handle(Future.succeededFuture());
    return;
  }
  PgPool clientToClose = client;
  client = null;
  connectionPool.removeMultiKey(vertx, tenantId);  // remove (vertx, tenantId, this) entry
  clientToClose.close();
  whenDone.handle(Future.succeededFuture());
}
 
Example 7
Source Project: raml-module-builder   Source File: PostgresClient.java    License: 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 8
private void sendQueries(PgPool p, int queries, long con, long seq) {
	int q = queries;
	
	while (--q >= 0) {
		
			final ResultObject target = DBRestInFlight.headObject();
			
			//already released but not published yet: TODO: we have a problem here!!!
			assert(null!=target && -1==target.getStatus()) : "found status "+target.getStatus()+" on query "+q+" of "+queries ; //must block that this has been consumed?? should head/tail rsolve.
							
			target.setConnectionId(con);
			target.setSequenceId(seq);
			assert(target.getStatus()==-1);//waiting for work
			target.setStatus(-2);//out for work	
			target.setGroupSize(queries);
		
			p.preparedQuery("SELECT * FROM world WHERE id=$1", Tuple.of(randomValue()), r -> {
					if (r.succeeded()) {

						RowIterator<Row> resultSet = r.result().iterator();
						Tuple row = resultSet.next();			        
						
						target.setId(row.getInteger(0));
						target.setResult(row.getInteger(1));					
						target.setStatus(200);

					} else {
						System.out.println("fail: "+r.cause().getLocalizedMessage());
						target.setStatus(500); 
					}		
					
				});	
						
			DBRestInFlight.moveHeadForward(); //always move to ensure this can be read.
	
	}
}
 
Example 9
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 10
Source Project: quarkus   Source File: PgPoolBuildItem.java    License: Apache License 2.0 4 votes vote down vote up
public PgPoolBuildItem(RuntimeValue<PgPool> pgPool) {
    this.pgPool = pgPool;
}
 
Example 11
Source Project: quarkus   Source File: PgPoolBuildItem.java    License: Apache License 2.0 4 votes vote down vote up
public RuntimeValue<PgPool> getPgPool() {
    return pgPool;
}
 
Example 12
Source Project: quarkus   Source File: ReactivePgClientProcessor.java    License: Apache License 2.0 4 votes vote down vote up
@BuildStep
@Record(ExecutionTime.RUNTIME_INIT)
ServiceStartBuildItem build(BuildProducer<FeatureBuildItem> feature,
        BuildProducer<PgPoolBuildItem> pgPool,
        BuildProducer<VertxPoolBuildItem> vertxPool,
        PgPoolRecorder recorder,
        VertxBuildItem vertx,
        BuildProducer<SyntheticBeanBuildItem> syntheticBeans,
        ShutdownContextBuildItem shutdown,
        BuildProducer<ExtensionSslNativeSupportBuildItem> sslNativeSupport,
        DataSourcesBuildTimeConfig dataSourcesBuildTimeConfig, DataSourcesRuntimeConfig dataSourcesRuntimeConfig,
        DataSourceReactiveBuildTimeConfig dataSourceReactiveBuildTimeConfig,
        DataSourceReactiveRuntimeConfig dataSourceReactiveRuntimeConfig,
        DataSourceReactivePostgreSQLConfig dataSourceReactivePostgreSQLConfig,
        LegacyDataSourcesRuntimeConfig legacyDataSourcesRuntimeConfig,
        LegacyDataSourceReactivePostgreSQLConfig legacyDataSourceReactivePostgreSQLConfig) {

    feature.produce(new FeatureBuildItem(Feature.REACTIVE_PG_CLIENT));
    // Make sure the PgPoolProducer is initialized before the StartupEvent is fired
    ServiceStartBuildItem serviceStart = new ServiceStartBuildItem("reactive-pg-client");

    // Note: we had to tweak that logic to support the legacy configuration
    if (dataSourcesBuildTimeConfig.defaultDataSource.dbKind.isPresent()
            && (!DatabaseKind.isPostgreSQL(dataSourcesBuildTimeConfig.defaultDataSource.dbKind.get())
                    || !dataSourceReactiveBuildTimeConfig.enabled)) {
        return serviceStart;
    }

    boolean isLegacy = !dataSourcesBuildTimeConfig.defaultDataSource.dbKind.isPresent();

    RuntimeValue<PgPool> pool = recorder.configurePgPool(vertx.getVertx(),
            dataSourcesRuntimeConfig, dataSourceReactiveRuntimeConfig, dataSourceReactivePostgreSQLConfig,
            legacyDataSourcesRuntimeConfig, legacyDataSourceReactivePostgreSQLConfig, isLegacy,
            shutdown);
    pgPool.produce(new PgPoolBuildItem(pool));

    // Synthetic bean for PgPool
    syntheticBeans.produce(
            SyntheticBeanBuildItem.configure(PgPool.class).addType(Pool.class).scope(Singleton.class).runtimeValue(pool)
                    .setRuntimeInit().done());

    boolean isDefault = true; // assume always the default pool for now
    vertxPool.produce(new VertxPoolBuildItem(pool, DatabaseKind.POSTGRESQL, isDefault));

    // Enable SSL support by default
    sslNativeSupport.produce(new ExtensionSslNativeSupportBuildItem(Feature.REACTIVE_PG_CLIENT));

    return serviceStart;
}
 
Example 13
Source Project: quarkus   Source File: PgPoolProducer.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * @return the <em>mutiny</em> PostGreSQL Pool instance. The instance is created lazily.
 */
@Singleton
@Produces
public io.vertx.mutiny.pgclient.PgPool mutinyPgPool() {
    return io.vertx.mutiny.pgclient.PgPool.newInstance(pgPool);
}
 
Example 14
Source Project: quarkus   Source File: ThreadLocalPgPool.java    License: Apache License 2.0 4 votes vote down vote up
@Override
protected PgPool createThreadLocalPool() {
    return PgPool.pool(vertx, pgConnectOptions, poolOptions);
}
 
Example 15
Source Project: quarkus   Source File: ReactivePgDataSourceHealthCheck.java    License: Apache License 2.0 4 votes vote down vote up
@PostConstruct
protected void init() {
    pgPool = Arc.container().instance(PgPool.class).get();
}
 
Example 16
Source Project: vertx-sql-client   Source File: PgDriver.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public Pool createPool(SqlConnectOptions options, PoolOptions poolOptions) {
  return PgPool.pool(wrap(options), poolOptions);
}
 
Example 17
Source Project: vertx-sql-client   Source File: PgDriver.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public Pool createPool(Vertx vertx, SqlConnectOptions options, PoolOptions poolOptions) {
  return PgPool.pool(vertx, wrap(options), poolOptions);
}
 
Example 18
Source Project: vertx-sql-client   Source File: PgTransactionTest.java    License: 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 19
Source Project: vertx-sql-client   Source File: PgTransactionTest.java    License: 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 20
Source Project: vertx-sql-client   Source File: PgTracingTest.java    License: Apache License 2.0 4 votes vote down vote up
@Override
protected Pool createPool(Vertx vertx) {
  return PgPool.pool(vertx, rule.options(), new PoolOptions());
}
 
Example 21
Source Project: okapi   Source File: PostgresHandle.java    License: 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 22
Source Project: raml-module-builder   Source File: PostgresClient.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * @return this instance's PgPool that allows connections to be made
 */
PgPool getClient() {
  return client;
}
 
Example 23
Source Project: raml-module-builder   Source File: PostgresClientHelper.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * For testing only circumvent the private visibility of PostgresClient.getClient().
 */
public static final PgPool getClient(PostgresClient postgresClient) {
  return postgresClient.getClient();
}
 
Example 24
Source Project: vertx-jooq   Source File: ReactiveDatabaseClientProvider.java    License: MIT License 4 votes vote down vote up
private ReactiveDatabaseClientProvider() {
    this.vertx = Vertx.vertx();
    this.pgClient = PgPool.pool(vertx, getOptions(), new PoolOptions());
    this.rxPgClient = new io.vertx.reactivex.sqlclient.Pool(pgClient);
}
 
Example 25
public PgPool pool() {
	if (null==pool) {			
		pool = PgPool.pool(vertx, options, poolOptions);			
	}
	return pool;
}
 
Example 26
Source Project: raml-module-builder   Source File: PostgresClient.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * Set this instance's PgPool that can connect to Postgres.
 * @param client  the new client
 */
void setClient(PgPool client) {
  this.client = client;
}