@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 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 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; } } }
@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()); }
@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 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()); }
@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()); }