org.davidmoten.rx.jdbc.pool.Pools Java Examples

The following examples show how to use org.davidmoten.rx.jdbc.pool.Pools. 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: DatabaseTest.java    From rxjava2-jdbc with Apache License 2.0 6 votes vote down vote up
@Test
public void testSelectUsingNonBlockingBuilder() {
    NonBlockingConnectionPool pool = Pools //
            .nonBlocking() //
            .connectionProvider(DatabaseCreator.connectionProvider()) //
            .maxIdleTime(1, TimeUnit.MINUTES) //
            .idleTimeBeforeHealthCheck(1, TimeUnit.MINUTES) //
            .connectionRetryInterval(1, TimeUnit.SECONDS) //
            .healthCheck(c -> c.prepareStatement("select 1").execute()) //
            .maxPoolSize(3) //
            .build();

    try (Database db = Database.from(pool)) {
        db.select("select score from person where name=?") //
                .parameters("FRED", "JOSEPH") //
                .getAs(Integer.class) //
                .test() //
                .awaitDone(TIMEOUT_SECONDS, TimeUnit.SECONDS) //
                .assertNoErrors() //
                .assertValues(21, 34) //
                .assertComplete();
    }
}
 
Example #2
Source File: DatabaseTest.java    From rxjava2-jdbc with Apache License 2.0 6 votes vote down vote up
@Test
public void testShutdownBeforeUse() {
    NonBlockingConnectionPool pool = Pools //
            .nonBlocking() //
            .connectionProvider(DatabaseCreator.connectionProvider()) //
            .scheduler(Schedulers.io()) //
            .maxPoolSize(1) //
            .build();
    pool.close();
    Database.from(pool) //
            .select("select score from person where name=?") //
            .parameter("FRED") //
            .getAs(Integer.class) //
            .test().awaitDone(TIMEOUT_SECONDS, TimeUnit.SECONDS) //
            .assertNoValues() //
            .assertError(PoolClosedException.class);
}
 
Example #3
Source File: EmployeeRepository.java    From webflux-rxjava2-jdbc-example with Apache License 2.0 5 votes vote down vote up
public EmployeeRepository() throws Exception {
  Connection connection = DriverManager.getConnection("jdbc:h2:./build/mydatabase", "sa", "sa");
  NonBlockingConnectionPool pool =
      Pools.nonBlocking()
          .maxPoolSize(Runtime.getRuntime().availableProcessors() * 5)
          .connectionProvider(ConnectionProvider.from(connection))
          .build();

  this.db = Database.from(pool);
}
 
Example #4
Source File: Database.java    From rxjava2-jdbc with Apache License 2.0 5 votes vote down vote up
public static Database from(@Nonnull String url, int maxPoolSize) {
    Preconditions.checkNotNull(url, "url cannot be null");
    Preconditions.checkArgument(maxPoolSize > 0, "maxPoolSize must be greater than 0");
    NonBlockingConnectionPool pool = Pools.nonBlocking() //
            .url(url) //
            .maxPoolSize(maxPoolSize) //
            .build();
    return Database.from( //
            pool, //
            () -> {
                pool.close();
            });
}
 
Example #5
Source File: Database.java    From rxjava2-jdbc with Apache License 2.0 5 votes vote down vote up
public static Database test(int maxPoolSize) {
    Preconditions.checkArgument(maxPoolSize > 0, "maxPoolSize must be greater than 0");
    return Database.from( //
            Pools.nonBlocking() //
                    .connectionProvider(testConnectionProvider()) //
                    .maxPoolSize(maxPoolSize) //
                    .build());
}
 
Example #6
Source File: DatabaseTest.java    From rxjava2-jdbc with Apache License 2.0 5 votes vote down vote up
@Test
public void testSelectSpecifyingHealthCheckAsSql() {
    final AtomicInteger success = new AtomicInteger();
    NonBlockingConnectionPool pool = Pools //
            .nonBlocking() //
            .connectionProvider(DatabaseCreator.connectionProvider()) //
            .maxIdleTime(1, TimeUnit.MINUTES) //
            .healthCheck(DatabaseType.H2) //
            .idleTimeBeforeHealthCheck(1, TimeUnit.MINUTES) //
            .connectionRetryInterval(1, TimeUnit.SECONDS) //
            .healthCheck("select 1") //
            .connectionListener(error -> {
                if (error.isPresent()) {
                    success.set(Integer.MIN_VALUE);
                } else {
                    success.incrementAndGet();
                }
            }) //
            .maxPoolSize(3) //
            .build();

    try (Database db = Database.from(pool)) {
        db.select("select score from person where name=?") //
                .parameters("FRED", "JOSEPH") //
                .getAs(Integer.class) //
                .test() //
                .awaitDone(TIMEOUT_SECONDS, TimeUnit.SECONDS) //
                .assertNoErrors() //
                .assertValues(21, 34) //
                .assertComplete();
    }
    assertEquals(1, success.get());
}
 
Example #7
Source File: DatabaseTest.java    From rxjava2-jdbc with Apache License 2.0 5 votes vote down vote up
private void testHealthCheck(Predicate<Connection> healthy) throws InterruptedException {
    TestScheduler scheduler = new TestScheduler();

    NonBlockingConnectionPool pool = Pools //
            .nonBlocking() //
            .connectionProvider(DatabaseCreator.connectionProvider()) //
            .maxIdleTime(10, TimeUnit.MINUTES) //
            .idleTimeBeforeHealthCheck(0, TimeUnit.MINUTES) //
            .healthCheck(healthy) //
            .scheduler(scheduler) //
            .maxPoolSize(1) //
            .build();

    try (Database db = Database.from(pool)) {
        TestSubscriber<Integer> ts0 = db.select( //
                "select score from person where name=?") //
                .parameter("FRED") //
                .getAs(Integer.class) //
                .test();
        ts0.assertValueCount(0) //
                .assertNotComplete();
        scheduler.advanceTimeBy(1, TimeUnit.MINUTES);
        ts0.assertValueCount(1) //
                .assertComplete();
        TestSubscriber<Integer> ts = db.select( //
                "select score from person where name=?") //
                .parameter("FRED") //
                .getAs(Integer.class) //
                .test() //
                .assertValueCount(0);
        log.debug("done2");
        scheduler.advanceTimeBy(1, TimeUnit.MINUTES);
        Thread.sleep(200);
        ts.assertValueCount(1);
        Thread.sleep(200);
        ts.assertValue(21) //
                .assertComplete();
    }
}
 
Example #8
Source File: RxJdbcConfig.java    From FrameworkBenchmarks with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Bean
public Database database(DataSourceProperties dsProps) throws SQLException {
    NonBlockingConnectionPool pool =
            Pools.nonBlocking()
                    .maxPoolSize(Runtime.getRuntime().availableProcessors() * 2)
                    .connectionProvider(ConnectionProvider.from(dsProps.getUrl(), dsProps.getUsername(), dsProps.getPassword()))
                    .build();

    Database db = Database.from(pool);

    return db;
}
 
Example #9
Source File: DatabaseTest.java    From rxjava2-jdbc with Apache License 2.0 4 votes vote down vote up
@Test(expected = IllegalArgumentException.class)
public void testNonBlockingPoolWithTrampolineSchedulerThrows() {
    Pools.nonBlocking().scheduler(Schedulers.trampoline());
}