use of com.cadenzauk.core.lang.CompositeAutoCloseable in project siesta by cadenzauk.
the class JdbcSqlExecutorTest method streamWhenExecuteThrows.
@Test
void streamWhenExecuteThrows() throws SQLException {
CompositeAutoCloseable closeable = new CompositeAutoCloseable();
when(preparedStatement.executeQuery()).thenReturn(resultSet);
when(preparedStatement.executeQuery()).thenThrow(new SQLException("No permission"));
JdbcSqlExecutor sut = JdbcSqlExecutor.of(dataSource);
String sql = "select name from foo where bar = 'fred'";
calling(() -> {
try (Stream<String> stream = sut.stream(connection, sql, toArray(), rowMapper, closeable)) {
return stream.collect(Collectors.toList());
}
}).shouldThrow(RuntimeException.class).withCause(SQLException.class).withMessage(is("No permission"));
verify(connection).prepareStatement(sql);
verify(preparedStatement).executeQuery();
verify(preparedStatement).setFetchSize(0);
verify(preparedStatement).close();
verifyNoMoreInteractions(connection, preparedStatement, resultSet, rowMapper);
}
use of com.cadenzauk.core.lang.CompositeAutoCloseable in project siesta by cadenzauk.
the class ExceptionIntegrationTest method lockTimeoutOnInsert.
@Test
void lockTimeoutOnInsert() {
assumeTrue(dialect.supportsLockTimeout(), "Database does not support lock timeouts.");
Database database = TestDatabase.testDatabase(dataSource);
SalesAreaRow salesArea = SalesAreaRow.newBuilder().salesAreaId(newId()).salesAreaName("NZ").build();
try (CompositeAutoCloseable closer = new CompositeAutoCloseable()) {
Transaction transaction1 = closer.add(database.beginTransaction());
database.insert(transaction1, salesArea);
Transaction transaction2 = closer.add(database.beginTransaction());
closer.add(database.withLockTimeout(transaction2, 0, TimeUnit.MILLISECONDS));
calling(() -> database.insert(transaction2, salesArea)).shouldThrow(RuntimeSqlException.class).with(subclass(LockingException.class)).withMessage(startsWith("Locking failure while executing 'insert into"));
transaction2.rollback();
}
}
use of com.cadenzauk.core.lang.CompositeAutoCloseable 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;
}
}
use of com.cadenzauk.core.lang.CompositeAutoCloseable in project siesta by cadenzauk.
the class JdbcSqlExecutorTest method streamWhenNextThrows.
@Test
void streamWhenNextThrows() throws SQLException {
CompositeAutoCloseable closeable = new CompositeAutoCloseable();
when(preparedStatement.executeQuery()).thenReturn(resultSet);
when(resultSet.next()).thenReturn(true).thenThrow(new SQLException("Connection lost"));
when(rowMapper.mapRow(resultSet)).thenReturn("Fred");
JdbcSqlExecutor sut = JdbcSqlExecutor.of(dataSource);
String sql = "select name from foo where bar = ?";
calling(() -> {
try (Stream<String> stream = sut.stream(connection, sql, toArray("Bob", "Burt"), rowMapper, closeable)) {
return stream.collect(Collectors.toList());
}
}).shouldThrow(RuntimeSqlException.class).withCause(SQLException.class).withMessage(is("Connection lost"));
verify(connection).prepareStatement(sql);
verify(preparedStatement).executeQuery();
verify(preparedStatement).setObject(1, "Bob");
verify(preparedStatement).setObject(2, "Burt");
verify(preparedStatement).setFetchSize(0);
verify(preparedStatement).close();
verify(resultSet, times(2)).next();
verify(resultSet).close();
verify(rowMapper).mapRow(resultSet);
verifyNoMoreInteractions(connection, preparedStatement, resultSet, rowMapper);
}
use of com.cadenzauk.core.lang.CompositeAutoCloseable in project siesta by cadenzauk.
the class JdbcSqlExecutorTest method streamWhenMapperThrows.
@Test
void streamWhenMapperThrows() throws SQLException {
CompositeAutoCloseable closeable = new CompositeAutoCloseable();
when(preparedStatement.executeQuery()).thenReturn(resultSet);
when(resultSet.next()).thenReturn(true);
when(rowMapper.mapRow(resultSet)).thenReturn("Fred").thenThrow(new IllegalArgumentException("Exception while mapping"));
JdbcSqlExecutor sut = JdbcSqlExecutor.of(dataSource);
String sql = "select name from foo where bar = ?";
calling(() -> {
try (Stream<String> stream = sut.stream(connection, sql, toArray("Bob", "Burt"), rowMapper, closeable)) {
return stream.collect(Collectors.toList());
}
}).shouldThrow(IllegalArgumentException.class).withMessage(is("Exception while mapping"));
verify(connection).prepareStatement(sql);
verify(preparedStatement).executeQuery();
verify(preparedStatement).setObject(1, "Bob");
verify(preparedStatement).setObject(2, "Burt");
verify(preparedStatement).setFetchSize(0);
verify(preparedStatement).close();
verify(resultSet, times(2)).next();
verify(resultSet).close();
verify(rowMapper, times(2)).mapRow(resultSet);
verifyNoMoreInteractions(connection, preparedStatement, resultSet, rowMapper);
}
Aggregations