Search in sources :

Example 1 with TransactedEventBus

use of io.syndesis.common.util.TransactedEventBus in project syndesis by syndesisio.

the class SqlJsonDB method withGlobalTransaction.

@Override
public void withGlobalTransaction(final Consumer<JsonDB> handler) {
    final String checkpoint = UUID.randomUUID().toString();
    try (Handle handle = dbi.open()) {
        handle.begin();
        handle.checkpoint(checkpoint);
        try (Connection connection = handle.getConnection();
            Connection transacted = withoutTransactionControl(connection)) {
            final TransactedEventBus transactedBus = new TransactedEventBus(bus);
            final SqlJsonDB checkpointed = new SqlJsonDB(new DBI(() -> transacted), transactedBus);
            boolean committed = false;
            try {
                handler.accept(checkpointed);
                handle.commit();
                committed = true;
                transactedBus.commit();
            } catch (@SuppressWarnings("PMD.AvoidCatchingGenericException") final RuntimeException e) {
                if (!committed) {
                    // if event bus blows up we can't rollback as the transaction has already been committed
                    handle.rollback(checkpoint);
                }
                throw SyndesisServerException.launderThrowable(e);
            }
        } catch (SQLException e) {
            throw SyndesisServerException.launderThrowable(e);
        }
    }
}
Also used : TransactedEventBus(io.syndesis.common.util.TransactedEventBus) SQLException(java.sql.SQLException) Connection(java.sql.Connection) DBI(org.skife.jdbi.v2.DBI) Handle(org.skife.jdbi.v2.Handle)

Aggregations

TransactedEventBus (io.syndesis.common.util.TransactedEventBus)1 Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 DBI (org.skife.jdbi.v2.DBI)1 Handle (org.skife.jdbi.v2.Handle)1