Search in sources :

Example 1 with SQLRuntimeException

use of org.davidmoten.rx.jdbc.exceptions.SQLRuntimeException 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 SQLRuntimeException

use of org.davidmoten.rx.jdbc.exceptions.SQLRuntimeException 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)

Example 3 with SQLRuntimeException

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

the class Tuples method toTupleN.

private static <T> TupleN<T> toTupleN(final Class<T> cls, ResultSet rs) {
    try {
        int n = rs.getMetaData().getColumnCount();
        List<T> list = new ArrayList<T>();
        for (int i = 1; i <= n; i++) {
            list.add(mapObject(rs, cls, i));
        }
        return new TupleN<T>(list);
    } catch (SQLException e) {
        throw new SQLRuntimeException(e);
    }
}
Also used : SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) SQLRuntimeException(org.davidmoten.rx.jdbc.exceptions.SQLRuntimeException)

Example 4 with SQLRuntimeException

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

the class Database method createDatabase.

private static void createDatabase(Connection c) {
    try {
        Sql.statements(Database.class.getResourceAsStream("/database-test.sql")).stream().forEach(x -> {
            try {
                c.prepareStatement(x).execute();
            } catch (SQLException e) {
                throw new SQLRuntimeException(e);
            }
        });
        c.commit();
    } catch (SQLException e) {
        throw new SQLRuntimeException(e);
    }
}
Also used : SQLException(java.sql.SQLException) SQLRuntimeException(org.davidmoten.rx.jdbc.exceptions.SQLRuntimeException)

Example 5 with SQLRuntimeException

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

the class DatabaseTest method testIssue20AutoCommitEnabledAndConnectionThrowsOnCommit.

@Test
public void testIssue20AutoCommitEnabledAndConnectionThrowsOnCommit() {
    ConnectionProvider cp = DatabaseCreator.connectionProvider();
    Database db = Database.fromBlocking(new ConnectionProvider() {

        @Override
        public Connection get() {
            Connection c = cp.get();
            try {
                c.setAutoCommit(true);
            } catch (SQLException e) {
                throw new SQLRuntimeException(e);
            }
            return new DelegatedConnection() {

                @Override
                public Connection con() {
                    return c;
                }

                @Override
                public void commit() throws SQLException {
                    System.out.println("COMMITTING");
                    if (this.getAutoCommit()) {
                        throw new SQLException("cannot commit when autoCommit is true");
                    } else {
                        con().commit();
                    }
                }
            };
        }

        @Override
        public void close() {
        // do nothing
        }
    });
    // 
    db.update("insert into note(text) values(?)").parameters("HI", // 
    "THERE").returnGeneratedKeys().getAs(// 
    Integer.class).test().awaitDone(TIMEOUT_SECONDS, // 
    TimeUnit.SECONDS).assertValues(1, // 
    2).assertComplete();
}
Also used : SQLException(java.sql.SQLException) JdbcSQLException(org.h2.jdbc.JdbcSQLException) DelegatedConnection(org.davidmoten.rx.jdbc.internal.DelegatedConnection) Connection(java.sql.Connection) CannotForkTransactedConnection(org.davidmoten.rx.jdbc.exceptions.CannotForkTransactedConnection) DelegatedConnection(org.davidmoten.rx.jdbc.internal.DelegatedConnection) SQLRuntimeException(org.davidmoten.rx.jdbc.exceptions.SQLRuntimeException) Test(org.junit.Test)

Aggregations

SQLException (java.sql.SQLException)8 SQLRuntimeException (org.davidmoten.rx.jdbc.exceptions.SQLRuntimeException)8 Connection (java.sql.Connection)5 CountDownLatch (java.util.concurrent.CountDownLatch)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 ConnectionProvider (org.davidmoten.rx.jdbc.ConnectionProvider)3 TimeoutException (java.util.concurrent.TimeoutException)2 Scheduler (io.reactivex.Scheduler)1 Schedulers (io.reactivex.schedulers.Schedulers)1 IOException (java.io.IOException)1 StandardCharsets (java.nio.charset.StandardCharsets)1 DriverManager (java.sql.DriverManager)1 PreparedStatement (java.sql.PreparedStatement)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ExecutorService (java.util.concurrent.ExecutorService)1 Executors (java.util.concurrent.Executors)1 TimeUnit (java.util.concurrent.TimeUnit)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 IOUtils (org.apache.commons.io.IOUtils)1