Search in sources :

Example 1 with ConnectionProvider

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

the class DatabaseCreator method connectionProvider.

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() {
        // 
        }
    };
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SQLException(java.sql.SQLException) Connection(java.sql.Connection) CountDownLatch(java.util.concurrent.CountDownLatch) SQLRuntimeException(org.davidmoten.rx.jdbc.exceptions.SQLRuntimeException) ConnectionProvider(org.davidmoten.rx.jdbc.ConnectionProvider) TimeoutException(java.util.concurrent.TimeoutException)

Example 2 with ConnectionProvider

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

the class DatabaseCreator method connectionProviderDerby.

private static ConnectionProvider connectionProviderDerby(String url, boolean withStoredProcs) {
    Connection c;
    try {
        c = DriverManager.getConnection(url);
        createDatabaseDerby(c);
        if (withStoredProcs) {
            addStoredProcs(c);
        }
    } catch (SQLException e) {
        throw new SQLRuntimeException(e);
    }
    return new ConnectionProvider() {

        @Override
        public Connection get() {
            try {
                return DriverManager.getConnection(url);
            } catch (SQLException e) {
                throw new SQLRuntimeException(e);
            }
        }

        @Override
        public void close() {
        // 
        }
    };
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) SQLRuntimeException(org.davidmoten.rx.jdbc.exceptions.SQLRuntimeException) ConnectionProvider(org.davidmoten.rx.jdbc.ConnectionProvider)

Aggregations

Connection (java.sql.Connection)2 SQLException (java.sql.SQLException)2 ConnectionProvider (org.davidmoten.rx.jdbc.ConnectionProvider)2 SQLRuntimeException (org.davidmoten.rx.jdbc.exceptions.SQLRuntimeException)2 CountDownLatch (java.util.concurrent.CountDownLatch)1 TimeoutException (java.util.concurrent.TimeoutException)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1