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());
}
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();
}
Aggregations