@Override public void deleteById(final UUID definitionId, final InternalCallContext context) throws TagDefinitionApiException { try { transactionalSqlDao.execute(false, new EntitySqlDaoTransactionWrapper<Void>() { @Override public Void inTransaction(final EntitySqlDaoWrapperFactory entitySqlDaoWrapperFactory) throws Exception { final TagDefinitionSqlDao tagDefinitionSqlDao = entitySqlDaoWrapperFactory.become(TagDefinitionSqlDao.class); // Make sure the tag definition exists final TagDefinitionModelDao tagDefinition = tagDefinitionSqlDao.getById(definitionId.toString(), context); if (tagDefinition == null) { throw new TagDefinitionApiException(ErrorCode.TAG_DEFINITION_DOES_NOT_EXIST, definitionId); } // Make sure it is not used currently if (tagDefinitionSqlDao.tagDefinitionUsageCount(definitionId.toString(), context) > 0) { throw new TagDefinitionApiException(ErrorCode.TAG_DEFINITION_IN_USE, definitionId); } // Delete it tagDefinitionSqlDao.markTagDefinitionAsDeleted(definitionId.toString(), context); postBusEventFromTransaction(tagDefinition, tagDefinition, ChangeType.DELETE, entitySqlDaoWrapperFactory, context); return null; } }); } catch (final TransactionFailedException exception) { if (exception.getCause() instanceof TagDefinitionApiException) { throw (TagDefinitionApiException) exception.getCause(); } else { throw exception; } } }
@Override public <ReturnType> ReturnType inTransaction(final Handle handle, final TransactionCallback<ReturnType> callback) { int retriesRemaining = configuration.maxRetries; while (true) { try { return getDelegate().inTransaction(handle, callback); } catch (Exception e) { if (!isSqlState(configuration.serializationFailureSqlStates, e) || --retriesRemaining <= 0) { if (e instanceof RuntimeException) { throw (RuntimeException) e; } throw new TransactionFailedException(e); } if (e instanceof SQLException) { final String sqlState = ((SQLException) e).getSQLState(); log.warn("Restarting transaction due to SQLState {}, retries remaining {}", sqlState, retriesRemaining); } else { log.warn("Restarting transaction due to {}, retries remaining {}", e.toString(), retriesRemaining); } } } }
throw new TransactionFailedException( "Can't validate a transaction before commit", ex); throw new TransactionFailedException( "Trying to commit a transaction that is already aborted. " + "Because current transaction is aborted, commands including " +
@Override public <ReturnType> ReturnType inTransaction(final Handle handle, final TransactionCallback<ReturnType> callback) { int retriesRemaining = configuration.maxRetries; while (true) { try { return getDelegate().inTransaction(handle, callback); } catch (Exception e) { if (!isSqlState(configuration.serializationFailureSqlStates, e) || --retriesRemaining <= 0) { if (e instanceof RuntimeException) { throw (RuntimeException) e; } throw new TransactionFailedException(e); } if (e instanceof SQLException) { final String sqlState = ((SQLException) e).getSQLState(); log.warn("Restarting transaction due to SQLState {}, retries remaining {}", sqlState, retriesRemaining); } else { log.warn("Restarting transaction due to {}, retries remaining {}", e.toString(), retriesRemaining); } } } }
@Override public <ReturnType> ReturnType inTransaction(final Handle handle, final TransactionCallback<ReturnType> callback) { int retriesRemaining = configuration.maxRetries; while (true) { try { return getDelegate().inTransaction(handle, callback); } catch (final Exception e) { if (!isSqlState(configuration.serializationFailureSqlStates, e) || --retriesRemaining <= 0) { if (e instanceof RuntimeException) { throw (RuntimeException) e; } throw new TransactionFailedException(e); } if (e instanceof SQLException) { final String sqlState = ((SQLException) e).getSQLState(); log.warn("Restarting transaction due to SQLState {}, retries remaining {}", sqlState, retriesRemaining); } else { log.warn("Restarting transaction due to {}, retries remaining {}", e.toString(), retriesRemaining); } } } }
@Override public void deleteById(final UUID definitionId, final InternalCallContext context) throws TagDefinitionApiException { try { transactionalSqlDao.execute(false, new EntitySqlDaoTransactionWrapper<Void>() { @Override public Void inTransaction(final EntitySqlDaoWrapperFactory entitySqlDaoWrapperFactory) throws Exception { final TagDefinitionSqlDao tagDefinitionSqlDao = entitySqlDaoWrapperFactory.become(TagDefinitionSqlDao.class); // Make sure the tag definition exists final TagDefinitionModelDao tagDefinition = tagDefinitionSqlDao.getById(definitionId.toString(), context); if (tagDefinition == null) { throw new TagDefinitionApiException(ErrorCode.TAG_DEFINITION_DOES_NOT_EXIST, definitionId); } // Make sure it is not used currently if (tagDefinitionSqlDao.tagDefinitionUsageCount(definitionId.toString(), context) > 0) { throw new TagDefinitionApiException(ErrorCode.TAG_DEFINITION_IN_USE, definitionId); } // Delete it tagDefinitionSqlDao.markTagDefinitionAsDeleted(definitionId.toString(), context); postBusEventFromTransaction(tagDefinition, tagDefinition, ChangeType.DELETE, entitySqlDaoWrapperFactory, context); return null; } }); } catch (final TransactionFailedException exception) { if (exception.getCause() instanceof TagDefinitionApiException) { throw (TagDefinitionApiException) exception.getCause(); } else { throw exception; } } }
@Override public <ReturnType> ReturnType inTransaction(Handle handle, TransactionCallback<ReturnType> callback) { int retriesRemaining = configuration.maxRetries; while (true) { try { return getDelegate().inTransaction(handle, callback); } catch (Exception e) { if (!isSqlState(configuration.serializationFailureSqlStates, e) || --retriesRemaining <= 0) { if (e instanceof RuntimeException) { throw (RuntimeException) e; } throw new TransactionFailedException(e); } if (e instanceof SQLException) { final String sqlState = ((SQLException) e).getSQLState(); log.warn("Restarting transaction due to SQLState {}, retries remaining {}", sqlState, retriesRemaining); } else { log.warn("Restarting transaction due to {}, retries remaining {}", e.toString(), retriesRemaining); } } } }
@Override public void deleteById(final UUID definitionId, final InternalCallContext context) throws TagDefinitionApiException { try { transactionalSqlDao.execute(new EntitySqlDaoTransactionWrapper<Void>() { @Override public Void inTransaction(final EntitySqlDaoWrapperFactory<EntitySqlDao> entitySqlDaoWrapperFactory) throws Exception { final TagDefinitionSqlDao tagDefinitionSqlDao = entitySqlDaoWrapperFactory.become(TagDefinitionSqlDao.class); // Make sure the tag definition exists final TagDefinitionModelDao tagDefinition = tagDefinitionSqlDao.getById(definitionId.toString(), context); if (tagDefinition == null) { throw new TagDefinitionApiException(ErrorCode.TAG_DEFINITION_DOES_NOT_EXIST, definitionId); } // Make sure it is not used currently if (tagDefinitionSqlDao.tagDefinitionUsageCount(definitionId.toString(), context) > 0) { throw new TagDefinitionApiException(ErrorCode.TAG_DEFINITION_IN_USE, definitionId); } // Delete it tagDefinitionSqlDao.markTagDefinitionAsDeleted(definitionId.toString(), context); postBusEventFromTransaction(tagDefinition, tagDefinition, ChangeType.DELETE, entitySqlDaoWrapperFactory, context); return null; } }); } catch (TransactionFailedException exception) { if (exception.getCause() instanceof TagDefinitionApiException) { throw (TagDefinitionApiException) exception.getCause(); } else { throw exception; } } }
@Override public ParamServerClientConnection getConnection() { Handle handle = dbi.open(); try { handle.getConnection().setAutoCommit(false); } catch (SQLException ex) { throw new TransactionFailedException("Failed to set auto commit: " + false, ex); } handle.begin(); return new PostgresqlServerClientConnection(handle); }
throw new TransactionFailedException("Transaction failed do to exception being thrown " + "from within the callback. See cause " + "for the original exception.", e); throw new TransactionFailedException("Transaction failed due to transaction status being set " + "to rollback only.");
throw new TransactionFailedException("Transaction failed do to exception being thrown " + "from within the callback. See cause " + "for the original exception.", e); throw new TransactionFailedException("Transaction failed due to transaction status being set " + "to rollback only.");
@Test public void testTxFail() throws Exception { Dao dao = handle.attach(Dao.class); try { dao.failed(1, "Ian"); fail("should have raised exception"); } catch (TransactionFailedException e) { assertThat(e.getCause().getMessage(), equalTo("woof")); } assertThat(dao.findById(1), nullValue()); }
public <T, E1 extends Exception, E2 extends Exception, E3 extends Exception> T transaction( TransactionActionWithExceptions<T, D, E1, E2, E3> action, Class<E1> exClass1, Class<E2> exClass2, Class<E3> exClass3) throws E1, E2, E3 { try { Handle handle = transactionManager.getHandle(configMapper); return action.call(handle, handle.attach(daoIface)); } catch (Exception ex) { Throwables.propagateIfInstanceOf(ex, exClass1); Throwables.propagateIfInstanceOf(ex, exClass2); Throwables.propagateIfInstanceOf(ex, exClass3); Throwables.propagateIfPossible(ex); throw new TransactionFailedException( "Transaction failed due to exception being thrown " + "from within the callback. See cause " + "for the original exception.", ex); } }
@Test public void testTxFail() throws Exception { Dao dao = handle.attach(Dao.class); try { dao.failed(1, "Ian"); fail("should have raised exception"); } catch (TransactionFailedException e) { assertThat(e.getCause().getMessage(), equalTo("woof")); } assertThat(dao.findById(1), nullValue()); }
throw new TransactionFailedException("Failed to set auto commit: " + autoAutoCommit, ex);
@Test public void testEventuallyFails() throws Exception { final AtomicInteger tries = new AtomicInteger(5); Handle handle = openHandle(); try { handle.inTransaction(TransactionIsolationLevel.SERIALIZABLE, new TransactionCallback<Void>() { @Override public Void inTransaction(Handle conn, TransactionStatus status) throws Exception { tries.decrementAndGet(); throw new SQLException("serialization", "40001"); } }); } catch (TransactionFailedException e) { Assert.assertEquals("40001", ((SQLException) e.getCause()).getSQLState()); } Assert.assertEquals(0, tries.get()); }
@Test public void testEventuallyFails() throws Exception { final AtomicInteger tries = new AtomicInteger(5); Handle handle = openHandle(); try { handle.inTransaction(TransactionIsolationLevel.SERIALIZABLE, new TransactionCallback<Void>() { @Override public Void inTransaction(Handle conn, TransactionStatus status) throws Exception { tries.decrementAndGet(); throw new SQLException("serialization", "40001"); } }); } catch (TransactionFailedException e) { Assert.assertEquals("40001", ((SQLException) e.getCause()).getSQLState()); } Assert.assertEquals(0, tries.get()); }