use of com.cadenzauk.core.sql.RuntimeSqlException in project siesta by cadenzauk.
the class JdbcSqlExecutor method stream.
<T> Stream<T> stream(Connection connection, String sql, Object[] args, RowMapper<T> rowMapper, CompositeAutoCloseable closeable) {
try {
PreparedStatement preparedStatement = prepare(connection, sql, args, closeable);
preparedStatement.setFetchSize(fetchSize);
ResultSet resultSet = closeable.add(preparedStatement.executeQuery());
return StreamSupport.stream(new ResultSetSpliterator<>(resultSet, rowMapper, closeable::close), false).onClose(closeable::close);
} catch (RuntimeException e) {
closeable.close();
throw e;
} catch (SQLException e) {
closeable.close();
throw new RuntimeSqlException(e);
} catch (Exception e) {
closeable.close();
throw new RuntimeException(e);
}
}
use of com.cadenzauk.core.sql.RuntimeSqlException in project siesta by cadenzauk.
the class AutoDetectDialect method from.
public static Dialect from(DataSource dataSource) {
try (Connection connection = DataSourceUtil.connection(dataSource)) {
DatabaseMetaData metaData = ConnectionUtil.getMetaData(connection);
String productName = metaData.getDatabaseProductName();
return DIALECTS.stream().filter(x -> x.item1().test(productName)).map(x -> x.item2().get()).findFirst().orElseGet(AnsiDialect::new);
} catch (SQLException e) {
throw new RuntimeSqlException(e);
}
}
use of com.cadenzauk.core.sql.RuntimeSqlException in project siesta by cadenzauk.
the class LockingIntegrationTest method performUpdate.
private boolean performUpdate(boolean setLockTimeout, Database database, long id, Synchronization synchronization) {
try (CompositeAutoCloseable closeable = new CompositeAutoCloseable()) {
synchronization.waitToSelect();
Transaction transaction = closeable.add(database.beginTransaction());
Optional<Integer> currentRevision = database.from(LockTestRow.class).select(LockTestRow::revision).where(LockTestRow::id).isEqualTo(id).optional(transaction);
LOG.info("Current revision = {}", currentRevision);
Optional<Integer> uncommittedRevision = database.from(LockTestRow.class).select(LockTestRow::revision).where(LockTestRow::id).isEqualTo(id).withIsolation(IsolationLevel.UNCOMMITTED_READ).optional(transaction);
LOG.info("Uncommitted revision = {}", uncommittedRevision);
if (uncommittedRevision.orElse(0) > currentRevision.orElse(0)) {
LOG.info("Updated by another transaction - could bail at this point", currentRevision, uncommittedRevision);
}
synchronization.waitToUpdate();
if (setLockTimeout) {
closeable.add(database.withLockTimeout(transaction, 0, TimeUnit.SECONDS));
}
int updateCount = currentRevision.map(curr -> database.update(LockTestRow.class).set(LockTestRow::updatedBy).to(Thread.currentThread().getName()).set(LockTestRow::revision).to(curr + 1).where(LockTestRow::id).isEqualTo(id).and(LockTestRow::revision).isEqualTo(curr).execute(transaction)).orElseGet(() -> database.insert(transaction, new LockTestRow(id, 1, Thread.currentThread().getName())));
LOG.info("Update count = {}", updateCount);
if (updateCount == 0) {
synchronization.finished();
return false;
}
synchronization.waitToCommit();
transaction.commit();
LOG.info("Committed");
synchronization.finished();
return true;
} catch (RuntimeSqlException e) {
LOG.error("Update failed", e);
synchronization.updateFailed();
return false;
}
}
Aggregations