io.vertx.sqlclient.PoolOptions Java Examples

The following examples show how to use io.vertx.sqlclient.PoolOptions. 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: SqlClientPool.java    From hibernate-reactive with GNU Lesser General Public License v2.1 6 votes vote down vote up
protected Pool configurePool(Map configurationValues, Vertx vertx) {
	URI uri = jdbcUrl(configurationValues);
	SqlConnectOptions connectOptions = sqlConnectOptions( uri );
	PoolOptions poolOptions = poolOptions( configurationValues );

	try {
		// First try to load the Pool using the standard ServiceLoader pattern
		// This only works if exactly 1 Driver is on the classpath.
		return Pool.pool( vertx, connectOptions, poolOptions );
	}
	catch (ServiceConfigurationError e) {
		// Backup option if multiple drivers are on the classpath.
		// We will be able to remove this once Vertx 3.9.2 is available
		return findDriver( uri, e ).createPool( vertx, connectOptions, poolOptions );
	}
}
 
Example #2
Source File: PgClientExamples.java    From vertx-sql-client with Apache License 2.0 6 votes vote down vote up
public void connecting01() {

    // Connect options
    PgConnectOptions connectOptions = new PgConnectOptions()
      .setPort(5432)
      .setHost("the-host")
      .setDatabase("the-db")
      .setUser("user")
      .setPassword("secret");

    // Pool options
    PoolOptions poolOptions = new PoolOptions()
      .setMaxSize(5);

    // Create the pooled client
    PgPool client = PgPool.pool(connectOptions, poolOptions);
  }
 
Example #3
Source File: PgClientExamples.java    From vertx-sql-client with Apache License 2.0 6 votes vote down vote up
public void configureFromDataObject(Vertx vertx) {

    // Data object
    PgConnectOptions connectOptions = new PgConnectOptions()
      .setPort(5432)
      .setHost("the-host")
      .setDatabase("the-db")
      .setUser("user")
      .setPassword("secret");

    // Pool Options
    PoolOptions poolOptions = new PoolOptions().setMaxSize(5);

    // Create the pool from the data object
    PgPool pool = PgPool.pool(vertx, connectOptions, poolOptions);

    pool.getConnection(ar -> {
      // Handling your connection
    });
  }
 
Example #4
Source File: MySQLClientExamples.java    From vertx-sql-client with Apache License 2.0 6 votes vote down vote up
public void connecting02(Vertx vertx) {

    // Connect options
    MySQLConnectOptions connectOptions = new MySQLConnectOptions()
      .setPort(3306)
      .setHost("the-host")
      .setDatabase("the-db")
      .setUser("user")
      .setPassword("secret");

    // Pool options
    PoolOptions poolOptions = new PoolOptions()
      .setMaxSize(5);
    // Create the pooled client
    MySQLPool client = MySQLPool.pool(vertx, connectOptions, poolOptions);
  }
 
Example #5
Source File: DB2ClientExamples.java    From vertx-sql-client with Apache License 2.0 6 votes vote down vote up
public void connecting02(Vertx vertx) {

    // Connect options
    DB2ConnectOptions connectOptions = new DB2ConnectOptions()
      .setPort(50000)
      .setHost("the-host")
      .setDatabase("the-db")
      .setUser("user")
      .setPassword("secret");

    // Pool options
    PoolOptions poolOptions = new PoolOptions()
      .setMaxSize(5);
    // Create the pooled client
    DB2Pool client = DB2Pool.pool(vertx, connectOptions, poolOptions);
  }
 
Example #6
Source File: DB2ClientExamples.java    From vertx-sql-client with Apache License 2.0 6 votes vote down vote up
public void connecting01() {

    // Connect options
    DB2ConnectOptions connectOptions = new DB2ConnectOptions()
      .setPort(50000)
      .setHost("the-host")
      .setDatabase("the-db")
      .setUser("user")
      .setPassword("secret");

    // Pool options
    PoolOptions poolOptions = new PoolOptions()
      .setMaxSize(5);

    // Create the pooled client
    DB2Pool client = DB2Pool.pool(connectOptions, poolOptions);
  }
 
Example #7
Source File: MySQLClientExamples.java    From vertx-sql-client with Apache License 2.0 6 votes vote down vote up
public void connecting01() {

    // Connect options
    MySQLConnectOptions connectOptions = new MySQLConnectOptions()
      .setPort(3306)
      .setHost("the-host")
      .setDatabase("the-db")
      .setUser("user")
      .setPassword("secret");

    // Pool options
    PoolOptions poolOptions = new PoolOptions()
      .setMaxSize(5);

    // Create the pooled client
    MySQLPool client = MySQLPool.pool(connectOptions, poolOptions);
  }
 
Example #8
Source File: PgPoolTest.java    From vertx-sql-client with Apache License 2.0 6 votes vote down vote up
@Test
public void testConnectionFailure(TestContext ctx) {
  Async async = ctx.async();
  ProxyServer proxy = ProxyServer.create(vertx, options.getPort(), options.getHost());
  AtomicReference<ProxyServer.Connection> proxyConn = new AtomicReference<>();
  proxy.proxyHandler(conn -> {
    proxyConn.set(conn);
    conn.connect();
  });
  PgPool pool = createPool(new PgConnectOptions(options).setPort(8080).setHost("localhost"),
    new PoolOptions()
      .setMaxSize(1)
      .setMaxWaitQueueSize(0)
  );
  pool.getConnection(ctx.asyncAssertFailure(err -> {
    proxy.listen(8080, "localhost", ctx.asyncAssertSuccess(v1 -> {
      pool.getConnection(ctx.asyncAssertSuccess(conn -> {
        async.complete();
      }));
    }));
  }));
}
 
Example #9
Source File: ApiTest.java    From vertx-in-action with MIT License 6 votes vote down vote up
@BeforeEach
void prepareDb(Vertx vertx, VertxTestContext testContext) {
  String insertQuery = "INSERT INTO stepevent VALUES($1, $2, $3::timestamp, $4)";
  LocalDateTime now = LocalDateTime.now();
  List<Tuple> data = Arrays.asList(
    Tuple.of("123", 1, LocalDateTime.of(2019, 4, 1, 23, 0), 6541),
    Tuple.of("123", 2, LocalDateTime.of(2019, 5, 20, 10, 0), 200),
    Tuple.of("123", 3, LocalDateTime.of(2019, 5, 21, 10, 10), 100),
    Tuple.of("456", 1, LocalDateTime.of(2019, 5, 21, 10, 15), 123),
    Tuple.of("123", 4, LocalDateTime.of(2019, 5, 21, 11, 0), 320),
    Tuple.of("abc", 1, now.minus(1, ChronoUnit.HOURS), 1000),
    Tuple.of("def", 1, now.minus(2, ChronoUnit.HOURS), 100),
    Tuple.of("def", 2, now.minus(30, ChronoUnit.MINUTES), 900),
    Tuple.of("abc", 2, now, 1500)
  );
  PgPool pgPool = PgPool.pool(vertx, PgConfig.pgConnectOpts(), new PoolOptions());

  pgPool.query("DELETE FROM stepevent")
    .rxExecute()
    .flatMap(rows -> pgPool.preparedQuery(insertQuery).rxExecuteBatch(data))
    .ignoreElement()
    .andThen(vertx.rxDeployVerticle(new ActivityApiVerticle()))
    .ignoreElement()
    .andThen(Completable.fromAction(pgPool::close))
    .subscribe(testContext::completeNow, testContext::failNow);
}
 
Example #10
Source File: EventProcessingTest.java    From vertx-in-action with MIT License 6 votes vote down vote up
@BeforeEach
void resetPgAndKafka(Vertx vertx, VertxTestContext testContext) {
  consumer = KafkaConsumer.create(vertx, KafkaConfig.consumer("activity-service-test-" + System.currentTimeMillis()));
  producer = KafkaProducer.create(vertx, KafkaConfig.producer());
  KafkaAdminClient adminClient = KafkaAdminClient.create(vertx, KafkaConfig.producer());

  PgPool pgPool = PgPool.pool(vertx, PgConfig.pgConnectOpts(), new PoolOptions());
  pgPool.query("DELETE FROM stepevent")
    .rxExecute()
    .flatMapCompletable(rs -> adminClient.rxDeleteTopics(Arrays.asList("incoming.steps", "daily.step.updates")))
    .andThen(Completable.fromAction(pgPool::close))
    .onErrorComplete()
    .subscribe(
      testContext::completeNow,
      testContext::failNow);
}
 
Example #11
Source File: EventsVerticle.java    From vertx-in-action with MIT License 6 votes vote down vote up
@Override
public Completable rxStart() {
  eventConsumer = KafkaConsumer.create(vertx, KafkaConfig.consumer("activity-service"));
  updateProducer = KafkaProducer.create(vertx, KafkaConfig.producer());
  pgPool = PgPool.pool(vertx, PgConfig.pgConnectOpts(), new PoolOptions());

  eventConsumer
    .subscribe("incoming.steps")
    .toFlowable()
    .flatMap(this::insertRecord)
    .flatMap(this::generateActivityUpdate)
    .flatMap(this::commitKafkaConsumerOffset)
    .doOnError(err -> logger.error("Woops", err))
    .retryWhen(this::retryLater)
    .subscribe();

  return Completable.complete();
}
 
Example #12
Source File: UnixDomainSocketTest.java    From vertx-sql-client with Apache License 2.0 6 votes vote down vote up
@Test
public void connectWithVertxInstance(TestContext context) {
  assumeTrue(options.isUsingDomainSocket());
  Vertx vertx = Vertx.vertx(new VertxOptions().setPreferNativeTransport(true));
  try {
    client = PgPool.pool(vertx, new PgConnectOptions(options), new PoolOptions());
    Async async = context.async();
    client.getConnection(context.asyncAssertSuccess(pgConnection -> {
      async.complete();
      pgConnection.close();
    }));
    async.await();
  } finally {
    vertx.close();
  }
}
 
Example #13
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 #14
Source File: MySQLTLSTest.java    From vertx-sql-client with Apache License 2.0 5 votes vote down vote up
@Test
public void testPoolFailWithVerifyCaSslMode(TestContext ctx) {
  options.setSslMode(SslMode.VERIFY_CA);
  options.setTrustAll(true);
  options.setPemKeyCertOptions(new PemKeyCertOptions()
    .setCertPath("tls/files/client-cert.pem")
    .setKeyPath("tls/files/client-key.pem"));

  try {
    MySQLPool.pool(vertx, options, new PoolOptions());
  } catch (IllegalArgumentException e) {
    ctx.assertEquals("Trust options must be specified under VERIFY_CA ssl-mode.", e.getMessage());
  }
}
 
Example #15
Source File: MSSQLPoolImpl.java    From vertx-sql-client with Apache License 2.0 5 votes vote down vote up
public static MSSQLPoolImpl create(ContextInternal context, boolean closeVertx, MSSQLConnectOptions connectOptions, PoolOptions poolOptions) {
  QueryTracer tracer = context.tracer() == null ? null : new QueryTracer(context.tracer(), connectOptions);
  VertxMetrics vertxMetrics = context.owner().metricsSPI();
  ClientMetrics metrics = vertxMetrics != null ? vertxMetrics.createClientMetrics(connectOptions.getSocketAddress(), "sql", connectOptions.getMetricsName()) : null;
  MSSQLPoolImpl pool = new MSSQLPoolImpl(context, new MSSQLConnectionFactory(context, connectOptions), tracer, metrics, poolOptions);
  CloseFuture closeFuture = pool.closeFuture();
  if (closeVertx) {
    closeFuture.onComplete(ar -> context.owner().close());
  } else {
    context.addCloseHook(closeFuture);
  }
  return pool;
}
 
Example #16
Source File: MySQLPool.java    From vertx-sql-client with Apache License 2.0 5 votes vote down vote up
/**
 * Create a connection pool to the MySQL server configured with the given {@code connectOptions} and {@code poolOptions}.
 *
 * @param connectOptions the options for the connection
 * @param poolOptions the options for creating the pool
 * @return the connection pool
 */
static MySQLPool pool(MySQLConnectOptions connectOptions, PoolOptions poolOptions) {
  if (Vertx.currentContext() != null) {
    throw new IllegalStateException("Running in a Vertx context => use MySQLPool#pool(Vertx, MySQLConnectOptions, PoolOptions) instead");
  }
  VertxOptions vertxOptions = new VertxOptions();
  Vertx vertx = Vertx.vertx(vertxOptions);
  return MySQLPoolImpl.create((ContextInternal) vertx.getOrCreateContext(), true, connectOptions, poolOptions);
}
 
Example #17
Source File: DriverTestBase.java    From vertx-sql-client with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreatePoolFromDriver04(TestContext ctx) {
  Pool p = getDriver().createPool(Vertx.vertx(), defaultOptions(), new PoolOptions().setMaxSize(1));
  p.getConnection(ctx.asyncAssertSuccess(ar -> {
    ar.close();
  }));
}
 
Example #18
Source File: MySQLClientExamples.java    From vertx-sql-client with Apache License 2.0 5 votes vote down vote up
public void gettingStarted() {

    // Connect options
    MySQLConnectOptions connectOptions = new MySQLConnectOptions()
      .setPort(3306)
      .setHost("the-host")
      .setDatabase("the-db")
      .setUser("user")
      .setPassword("secret");

    // Pool options
    PoolOptions poolOptions = new PoolOptions()
      .setMaxSize(5);

    // Create the client pool
    MySQLPool client = MySQLPool.pool(connectOptions, poolOptions);

    // A simple query
    client
      .query("SELECT * FROM users WHERE id='julien'")
      .execute(ar -> {
      if (ar.succeeded()) {
        RowSet<Row> result = ar.result();
        System.out.println("Got " + result.size() + " rows ");
      } else {
        System.out.println("Failure: " + ar.cause().getMessage());
      }

      // Now close the pool
      client.close();
    });
  }
 
Example #19
Source File: PgClientExamples.java    From vertx-sql-client with Apache License 2.0 5 votes vote down vote up
public void gettingStarted() {

    // Connect options
    PgConnectOptions connectOptions = new PgConnectOptions()
      .setPort(5432)
      .setHost("the-host")
      .setDatabase("the-db")
      .setUser("user")
      .setPassword("secret");

    // Pool options
    PoolOptions poolOptions = new PoolOptions()
      .setMaxSize(5);

    // Create the client pool
    PgPool client = PgPool.pool(connectOptions, poolOptions);

    // A simple query
    client
      .query("SELECT * FROM users WHERE id='julien'")
      .execute(ar -> {
      if (ar.succeeded()) {
        RowSet<Row> result = ar.result();
        System.out.println("Got " + result.size() + " rows ");
      } else {
        System.out.println("Failure: " + ar.cause().getMessage());
      }

      // Now close the pool
      client.close();
    });
  }
 
Example #20
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 #21
Source File: MySQLPoolRecorder.java    From quarkus with Apache License 2.0 5 votes vote down vote up
private MySQLPool initialize(Vertx vertx, DataSourceRuntimeConfig dataSourceRuntimeConfig,
        DataSourceReactiveRuntimeConfig dataSourceReactiveRuntimeConfig,
        DataSourceReactiveMySQLConfig dataSourceReactiveMySQLConfig) {
    PoolOptions poolOptions = toPoolOptions(dataSourceRuntimeConfig, dataSourceReactiveRuntimeConfig,
            dataSourceReactiveMySQLConfig);
    MySQLConnectOptions mysqlConnectOptions = toMySQLConnectOptions(dataSourceRuntimeConfig,
            dataSourceReactiveRuntimeConfig, dataSourceReactiveMySQLConfig);
    if (dataSourceReactiveRuntimeConfig.threadLocal.isPresent() &&
            dataSourceReactiveRuntimeConfig.threadLocal.get()) {
        return new ThreadLocalMySQLPool(vertx, mysqlConnectOptions, poolOptions);
    }
    return MySQLPool.pool(vertx, mysqlConnectOptions, poolOptions);
}
 
Example #22
Source File: DB2Pool.java    From vertx-sql-client with Apache License 2.0 5 votes vote down vote up
/**
 * Create a connection pool to the DB2 server configured with the given
 * {@code connectOptions} and {@code poolOptions}.
 *
 * @param connectOptions the options for the connection
 * @param poolOptions    the options for creating the pool
 * @return the connection pool
 */
static DB2Pool pool(DB2ConnectOptions connectOptions, PoolOptions poolOptions) {
  if (Vertx.currentContext() != null) {
    throw new IllegalStateException(
        "Running in a Vertx context => use DB2Pool#pool(Vertx, DB2ConnectOptions, PoolOptions) instead");
  }
  VertxOptions vertxOptions = new VertxOptions();
  Vertx vertx = Vertx.vertx(vertxOptions);
  return DB2PoolImpl.create((ContextInternal) vertx.getOrCreateContext(), true, connectOptions, poolOptions);
}
 
Example #23
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 #24
Source File: MySQLPoolImpl.java    From vertx-sql-client with Apache License 2.0 5 votes vote down vote up
public static MySQLPoolImpl create(ContextInternal context, boolean closeVertx, MySQLConnectOptions connectOptions, PoolOptions poolOptions) {
  QueryTracer tracer = context.tracer() == null ? null : new QueryTracer(context.tracer(), connectOptions);
  VertxMetrics vertxMetrics = context.owner().metricsSPI();
  ClientMetrics metrics = vertxMetrics != null ? vertxMetrics.createClientMetrics(connectOptions.getSocketAddress(), "sql", connectOptions.getMetricsName()) : null;
  MySQLPoolImpl pool = new MySQLPoolImpl(context, new MySQLConnectionFactory(context, connectOptions), tracer, metrics, poolOptions);
  CloseFuture closeFuture = pool.closeFuture();
  if (closeVertx) {
    closeFuture.onComplete(ar -> context.owner().close());
  } else {
    context.addCloseHook(closeFuture);
  }
  return pool;
}
 
Example #25
Source File: PgPoolImpl.java    From vertx-sql-client with Apache License 2.0 5 votes vote down vote up
public static PgPoolImpl create(ContextInternal context, boolean closeVertx, PgConnectOptions connectOptions, PoolOptions poolOptions) {
  QueryTracer tracer = context.tracer() == null ? null : new QueryTracer(context.tracer(), connectOptions);
  VertxMetrics vertxMetrics = context.owner().metricsSPI();
  ClientMetrics metrics = vertxMetrics != null ? vertxMetrics.createClientMetrics(connectOptions.getSocketAddress(), "sql", connectOptions.getMetricsName()) : null;
  PgPoolImpl pool = new PgPoolImpl(context, new PgConnectionFactory(context.owner(), context, connectOptions), tracer, metrics, poolOptions);
  CloseFuture closeFuture = pool.closeFuture();
  if (closeVertx) {
    closeFuture.onComplete(ar -> context.owner().close());
  } else {
    context.addCloseHook(closeFuture);
  }
  return pool;
}
 
Example #26
Source File: PgPoolTest.java    From vertx-sql-client with Apache License 2.0 5 votes vote down vote up
@Test
public void testRunWithExisting(TestContext ctx) {
  Async async = ctx.async();
  vertx.runOnContext(v -> {
    try {
      PgPool.pool(new PoolOptions());
      ctx.fail();
    } catch (IllegalStateException ignore) {
      async.complete();
    }
  });
}
 
Example #27
Source File: PgPoolTest.java    From vertx-sql-client with Apache License 2.0 5 votes vote down vote up
@Test
public void testRunStandalone(TestContext ctx) {
  Async async = ctx.async();
  PgPool pool = createPool(new PgConnectOptions(options), new PoolOptions());
  try {
    pool.query("SELECT id, randomnumber from WORLD").execute(ctx.asyncAssertSuccess(v -> {
      async.complete();
    }));
    async.await(4000);
  } finally {
    pool.close();
  }
}
 
Example #28
Source File: DB2ClientExamples.java    From vertx-sql-client with Apache License 2.0 5 votes vote down vote up
public void gettingStarted() {

    // Connect options
    DB2ConnectOptions connectOptions = new DB2ConnectOptions()
      .setPort(50000)
      .setHost("the-host")
      .setDatabase("the-db")
      .setUser("user")
      .setPassword("secret");

    // Pool options
    PoolOptions poolOptions = new PoolOptions()
      .setMaxSize(5);

    // Create the client pool
    DB2Pool client = DB2Pool.pool(connectOptions, poolOptions);

    // A simple query
    client
      .query("SELECT * FROM users WHERE id='julien'")
      .execute(ar -> {
      if (ar.succeeded()) {
        RowSet<Row> result = ar.result();
        System.out.println("Got " + result.size() + " rows ");
      } else {
        System.out.println("Failure: " + ar.cause().getMessage());
      }

      // Now close the pool
      client.close();
    });
  }
 
Example #29
Source File: PgPooledConnectionTest.java    From vertx-sql-client with Apache License 2.0 5 votes vote down vote up
public PgPooledConnectionTest() {
  connector = handler -> {
    if (pool == null) {
      pool = PgPool.pool(vertx, new PgConnectOptions(options), new PoolOptions().setMaxSize(1));
    }
    pool.getConnection(handler);
  };
}
 
Example #30
Source File: UnixDomainSocketTest.java    From vertx-sql-client with Apache License 2.0 5 votes vote down vote up
@Test
public void testIgnoreSslMode(TestContext context) {
  assumeTrue(options.isUsingDomainSocket());
  client = PgPool.pool(new PgConnectOptions(options).setSslMode(SslMode.REQUIRE), new PoolOptions());
  client.getConnection(context.asyncAssertSuccess(pgConnection -> {
    assertFalse(pgConnection.isSSL());
    pgConnection.close();
  }));
}