Search in sources :

Example 1 with RuntimeSqlException

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);
    }
}
Also used : ResultSetSpliterator(com.cadenzauk.core.sql.ResultSetSpliterator) RuntimeSqlException(com.cadenzauk.core.sql.RuntimeSqlException) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) SQLException(java.sql.SQLException) RuntimeSqlException(com.cadenzauk.core.sql.RuntimeSqlException)

Example 2 with RuntimeSqlException

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);
    }
}
Also used : Tuple2(com.cadenzauk.core.tuple.Tuple2) Connection(java.sql.Connection) Predicate(java.util.function.Predicate) Tuple(com.cadenzauk.core.tuple.Tuple) DatabaseMetaData(java.sql.DatabaseMetaData) Dialect(com.cadenzauk.siesta.Dialect) Supplier(java.util.function.Supplier) DataSourceUtil(com.cadenzauk.core.sql.DataSourceUtil) SQLException(java.sql.SQLException) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) RuntimeSqlException(com.cadenzauk.core.sql.RuntimeSqlException) DataSource(javax.sql.DataSource) Pattern(java.util.regex.Pattern) ConnectionUtil(com.cadenzauk.core.sql.ConnectionUtil) RuntimeSqlException(com.cadenzauk.core.sql.RuntimeSqlException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) DatabaseMetaData(java.sql.DatabaseMetaData)

Example 3 with RuntimeSqlException

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;
    }
}
Also used : Logger(org.slf4j.Logger) LockTestRow(com.cadenzauk.siesta.model.LockTestRow) TestDatabase(com.cadenzauk.siesta.model.TestDatabase) LoggerFactory(org.slf4j.LoggerFactory) CompletableFuture(java.util.concurrent.CompletableFuture) CompositeAutoCloseable(com.cadenzauk.core.lang.CompositeAutoCloseable) Test(org.junit.jupiter.api.Test) TimeUnit(java.util.concurrent.TimeUnit) RuntimeSqlException(com.cadenzauk.core.sql.RuntimeSqlException) Assumptions.assumeTrue(org.junit.jupiter.api.Assumptions.assumeTrue) Optional(java.util.Optional) Matchers.is(org.hamcrest.Matchers.is) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) RuntimeSqlException(com.cadenzauk.core.sql.RuntimeSqlException) CompositeAutoCloseable(com.cadenzauk.core.lang.CompositeAutoCloseable) LockTestRow(com.cadenzauk.siesta.model.LockTestRow)

Aggregations

RuntimeSqlException (com.cadenzauk.core.sql.RuntimeSqlException)3 SQLException (java.sql.SQLException)2 CompositeAutoCloseable (com.cadenzauk.core.lang.CompositeAutoCloseable)1 ConnectionUtil (com.cadenzauk.core.sql.ConnectionUtil)1 DataSourceUtil (com.cadenzauk.core.sql.DataSourceUtil)1 ResultSetSpliterator (com.cadenzauk.core.sql.ResultSetSpliterator)1 Tuple (com.cadenzauk.core.tuple.Tuple)1 Tuple2 (com.cadenzauk.core.tuple.Tuple2)1 Dialect (com.cadenzauk.siesta.Dialect)1 LockTestRow (com.cadenzauk.siesta.model.LockTestRow)1 TestDatabase (com.cadenzauk.siesta.model.TestDatabase)1 ImmutableList (com.google.common.collect.ImmutableList)1 Connection (java.sql.Connection)1 DatabaseMetaData (java.sql.DatabaseMetaData)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 List (java.util.List)1 Optional (java.util.Optional)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 TimeUnit (java.util.concurrent.TimeUnit)1