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