Search in sources :

Example 1 with NonBlockingConnectionPool

use of org.davidmoten.rx.jdbc.pool.NonBlockingConnectionPool in project rxjava2-jdbc by davidmoten.

the class DatabaseTest method testHealthCheck.

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();
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) NonBlockingConnectionPool(org.davidmoten.rx.jdbc.pool.NonBlockingConnectionPool) TestScheduler(io.reactivex.schedulers.TestScheduler)

Example 2 with NonBlockingConnectionPool

use of org.davidmoten.rx.jdbc.pool.NonBlockingConnectionPool in project rxjava2-jdbc by davidmoten.

the class DatabaseTest method testShutdownBeforeUse.

@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);
}
Also used : NonBlockingConnectionPool(org.davidmoten.rx.jdbc.pool.NonBlockingConnectionPool) Test(org.junit.Test)

Example 3 with NonBlockingConnectionPool

use of org.davidmoten.rx.jdbc.pool.NonBlockingConnectionPool in project rxjava2-jdbc by davidmoten.

the class Database method from.

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();
    });
}
Also used : NonBlockingConnectionPool(org.davidmoten.rx.jdbc.pool.NonBlockingConnectionPool)

Aggregations

NonBlockingConnectionPool (org.davidmoten.rx.jdbc.pool.NonBlockingConnectionPool)3 TestScheduler (io.reactivex.schedulers.TestScheduler)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Test (org.junit.Test)1