Search in sources :

Example 1 with DelegatedConnection

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

the class DatabaseTest method testFromDataSource.

@Test
public void testFromDataSource() {
    ConnectionProvider cp = DatabaseCreator.connectionProvider();
    AtomicInteger count = new AtomicInteger();
    Set<Connection> connections = new HashSet<>();
    AtomicInteger closed = new AtomicInteger();
    Database db = Database.fromBlocking(new ConnectionProvider() {

        @Override
        public Connection get() {
            count.incrementAndGet();
            Connection c = cp.get();
            connections.add(c);
            return new DelegatedConnection() {

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

                @Override
                public void close() {
                    closed.incrementAndGet();
                }
            };
        }

        @Override
        public void close() {
        // do nothing
        }
    });
    // 
    db.select("select count(*) from person").getAs(// 
    Integer.class).blockingSubscribe();
    // 
    db.select("select count(*) from person").getAs(// 
    Integer.class).blockingSubscribe();
    assertEquals(2, count.get());
    assertEquals(2, connections.size());
    assertEquals(2, closed.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) 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) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 2 with DelegatedConnection

use of org.davidmoten.rx.jdbc.internal.DelegatedConnection 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

Connection (java.sql.Connection)2 CannotForkTransactedConnection (org.davidmoten.rx.jdbc.exceptions.CannotForkTransactedConnection)2 DelegatedConnection (org.davidmoten.rx.jdbc.internal.DelegatedConnection)2 Test (org.junit.Test)2 SQLException (java.sql.SQLException)1 HashSet (java.util.HashSet)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 SQLRuntimeException (org.davidmoten.rx.jdbc.exceptions.SQLRuntimeException)1 JdbcSQLException (org.h2.jdbc.JdbcSQLException)1