Java Code Examples for org.davidmoten.rx.jdbc.ConnectionProvider

The following examples show how to use org.davidmoten.rx.jdbc.ConnectionProvider. 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
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 2
Source Project: rxjava2-jdbc   Source File: NonBlockingConnectionPool.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Sets the provider of {@link Connection} objects to be used by the pool.
 * 
 * @param cp
 *            connection provider
 * @return this
 */
public Builder<T> connectionProvider(ConnectionProvider cp) {
    Preconditions.checkArgument(!(cp instanceof SingletonConnectionProvider), //
            "connection provider should not return a singleton connection because " //
            + "a pool needs control over the creation and closing of connections. " //
            + "Use ConnectionProvider.from(url,...) instead.");
    this.cp = cp;
    return this;
}
 
Example 3
Source Project: rxjava2-jdbc   Source File: DatabaseCreator.java    License: Apache License 2.0 5 votes vote down vote up
private static ConnectionProvider connectionProvider(String url, boolean big) {
    return new ConnectionProvider() {

        private final AtomicBoolean once = new AtomicBoolean(false);
        private final CountDownLatch latch = new CountDownLatch(1);

        @Override
        public Connection get() {
            try {
                if (once.compareAndSet(false, true)) {
                    try {
                        Connection c = DriverManager.getConnection(url);
                        createDatabase(c, big);
                        c.setAutoCommit(false);
                        return c;
                    } finally {
                        latch.countDown();
                    }
                } else {
                    if (latch.await(30, TimeUnit.SECONDS)) {
                        return DriverManager.getConnection(url);
                    } else {
                        throw new TimeoutException("big database timed out on creation");
                    }
                }
            } catch (SQLException | InterruptedException | TimeoutException e) {
                throw new SQLRuntimeException(e);
            }
        }

        @Override
        public void close() {
            //
        }
    };
}
 
Example 4
@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 5
public ConnectionProviderBlockingPool(ConnectionProvider connectionProvider) {
    this.connectionProvider = connectionProvider;
}
 
Example 6
public MemberWithValueConnection(ConnectionProvider cp) {
    this.connectionProvider = cp;
}
 
Example 7
Source Project: rxjava2-jdbc   Source File: DatabaseCreator.java    License: Apache License 2.0 4 votes vote down vote up
public static ConnectionProvider connectionProvider() {
    return connectionProvider(nextUrl(), false);
}
 
Example 8
Source Project: rxjava2-jdbc   Source File: DatabaseCreator.java    License: Apache License 2.0 4 votes vote down vote up
public static ConnectionProvider connectionProviderDerby() {
    return connectionProvider(nextUrlDerby(), false);
}
 
Example 9
@Test(expected = IllegalArgumentException.class)
public void testRejectSingletonConnectionProvider() {
    Connection con = Mockito.mock(Connection.class);
    NonBlockingConnectionPool.builder().connectionProvider(ConnectionProvider.from(con));
}