@Test public void propagationMandatoryFailsInCaseOfNoExistingTransaction() { MockUOWManager manager = new MockUOWManager(); WebSphereUowTransactionManager ptm = new WebSphereUowTransactionManager(manager); DefaultTransactionDefinition definition = new DefaultTransactionDefinition(); definition.setPropagationBehavior(TransactionDefinition.PROPAGATION_MANDATORY); try { ptm.execute(definition, new TransactionCallback<String>() { @Override public String doInTransaction(TransactionStatus status) { return "result"; } }); fail("Should have thrown IllegalTransactionStateException"); } catch (IllegalTransactionStateException ex) { // expected } }
@Test public void propagationNeverFailsInCaseOfExistingTransaction() { MockUOWManager manager = new MockUOWManager(); manager.setUOWStatus(UOWManager.UOW_STATUS_ACTIVE); WebSphereUowTransactionManager ptm = new WebSphereUowTransactionManager(manager); DefaultTransactionDefinition definition = new DefaultTransactionDefinition(); definition.setPropagationBehavior(TransactionDefinition.PROPAGATION_NEVER); try { ptm.execute(definition, new TransactionCallback<String>() { @Override public String doInTransaction(TransactionStatus status) { return "result"; } }); fail("Should have thrown IllegalTransactionStateException"); } catch (IllegalTransactionStateException ex) { // expected } }
public void testPropagationMandatoryFailsInCaseOfNoExistingTransaction() { MockUOWManager manager = new MockUOWManager(); WebSphereUowTransactionManager ptm = new WebSphereUowTransactionManager(manager); DefaultTransactionDefinition definition = new DefaultTransactionDefinition(); definition.setPropagationBehavior(TransactionDefinition.PROPAGATION_MANDATORY); try { ptm.execute(definition, new TransactionCallback() { @Override public Object doInTransaction(TransactionStatus status) { return "result"; } }); fail("Should have thrown IllegalTransactionStateException"); } catch (IllegalTransactionStateException ex) { // expected } }
public void testPropagationNeverFailsInCaseOfExistingTransaction() { MockUOWManager manager = new MockUOWManager(); manager.setUOWStatus(UOWManager.UOW_STATUS_ACTIVE); WebSphereUowTransactionManager ptm = new WebSphereUowTransactionManager(manager); DefaultTransactionDefinition definition = new DefaultTransactionDefinition(); definition.setPropagationBehavior(TransactionDefinition.PROPAGATION_NEVER); try { ptm.execute(definition, new TransactionCallback() { @Override public Object doInTransaction(TransactionStatus status) { return "result"; } }); fail("Should have thrown IllegalTransactionStateException"); } catch (IllegalTransactionStateException ex) { // expected } }
/** * This implementation of commit handles participating in existing * transactions and programmatic rollback requests. * Delegates to {@code isRollbackOnly}, {@code doCommit} * and {@code rollback}. * @see org.springframework.transaction.TransactionStatus#isRollbackOnly() * @see #doCommit * @see #rollback */ @Override public final void commit(TransactionStatus status) throws TransactionException { if (status.isCompleted()) { throw new IllegalTransactionStateException( "Transaction is already completed - do not call commit or rollback more than once per transaction"); } DefaultTransactionStatus defStatus = (DefaultTransactionStatus) status; if (defStatus.isLocalRollbackOnly()) { if (defStatus.isDebug()) { logger.debug("Transactional code has requested rollback"); } processRollback(defStatus); return; } if (!shouldCommitOnGlobalRollbackOnly() && defStatus.isGlobalRollbackOnly()) { if (defStatus.isDebug()) { logger.debug("Global transaction is marked as rollback-only but transactional code requested commit"); } processRollback(defStatus); // Throw UnexpectedRollbackException only at outermost transaction boundary // or if explicitly asked to. if (status.isNewTransaction() || isFailEarlyOnGlobalRollbackOnly()) { throw new UnexpectedRollbackException( "Transaction rolled back because it has been marked as rollback-only"); } return; } processCommit(defStatus); }
/** * This implementation of rollback handles participating in existing * transactions. Delegates to {@code doRollback} and * {@code doSetRollbackOnly}. * @see #doRollback * @see #doSetRollbackOnly */ @Override public final void rollback(TransactionStatus status) throws TransactionException { if (status.isCompleted()) { throw new IllegalTransactionStateException( "Transaction is already completed - do not call commit or rollback more than once per transaction"); } DefaultTransactionStatus defStatus = (DefaultTransactionStatus) status; processRollback(defStatus); }
public void commit() { if (rollbackCalled) { throw new IllegalTransactionStateException("Cannot call commit after rollback. Start another transaction first!"); } try { /*final Object[] objects =*/ redisTemplate.exec(); commitCalled = true; } catch (Exception e) { throw new TransactionSystemException("Exception occurred committing back Redis transaction: " + e.getMessage()); } }
@Test public void testFlushWithoutTransaction() { try { new TransactionalAspectMethods().testFlushWithoutTransaction(); Assertions.failExceptionExpected(); } catch (final Throwable t) { Assertions.assertThat(t).isInstanceOf(IllegalTransactionStateException.class); } }
@Test public void testFlushWithoutTransaction() { Assertions.assertThrows(IllegalTransactionStateException.class, new Executable() { @Override public void execute() throws Throwable { new TransactionalAspectMethods().testFlushWithoutTransaction(); } }); }
@Test(expected = IllegalTransactionStateException.class) public void testMandatoryFailsWhenInvokedWithoutTx() { txUtil.executeWith_MANDATORY(new Runnable() { @Override public void run() { fail("Must never reach this point!"); } }); }
/** */ public void testMandatoryPropagation() { IgniteCache<Integer, String> c = grid().cache(CACHE_NAME); try { service.putWithMandatoryPropagation(c); } catch (IllegalTransactionStateException e) { assertEquals("No existing transaction found for transaction marked with propagation 'mandatory'", e.getMessage()); } assertEquals(0, c.size()); }
private Object proceed(ProceedingJoinPoint pjp, Retry retryAnnotation) throws Throwable { int times = retryAnnotation.times(); Class<? extends Throwable>[] retryOn = retryAnnotation.on(); Assert.isTrue(times > 0, "@Retry{times} should be greater than 0!"); Assert.isTrue(retryOn.length > 0, "@Retry{on} should have at least one Throwable!"); if (retryAnnotation.failInTransaction() && TransactionSynchronizationManager.isActualTransactionActive()) { throw new IllegalTransactionStateException( "You shouldn't retry an operation from within an existing Transaction." + "This is because we can't retry if the current Transaction was already rolled back!"); } LOGGER.info("Proceed with {} retries on {}", times, Arrays.toString(retryOn)); return tryProceeding(pjp, times, retryOn); }
@Test(expected = IllegalTransactionStateException.class) public void testRetryFailsOnTransaction() { try { TransactionSynchronizationManager.setActualTransactionActive(true); itemService.saveItem(); } finally { TransactionSynchronizationManager.setActualTransactionActive(false); } }
/** * This implementation of commit handles participating in existing * transactions and programmatic rollback requests. * Delegates to {@code isRollbackOnly}, {@code doCommit} * and {@code rollback}. * @see org.springframework.transaction.TransactionStatus#isRollbackOnly() * @see #doCommit * @see #rollback */ public final void commit(TransactionStatus status) throws TransactionException { if (status.isCompleted()) { throw new IllegalTransactionStateException( "Transaction is already completed - do not call commit or rollback more than once per transaction"); } DefaultTransactionStatus defStatus = (DefaultTransactionStatus) status; if (defStatus.isLocalRollbackOnly()) { if (defStatus.isDebug()) { logger.debug("Transactional code has requested rollback"); } processRollback(defStatus); return; } if (!shouldCommitOnGlobalRollbackOnly() && defStatus.isGlobalRollbackOnly()) { if (defStatus.isDebug()) { logger.debug("Global transaction is marked as rollback-only but transactional code requested commit"); } processRollback(defStatus); // Throw UnexpectedRollbackException only at outermost transaction boundary // or if explicitly asked to. if (status.isNewTransaction() || isFailEarlyOnGlobalRollbackOnly()) { throw new UnexpectedRollbackException( "Transaction rolled back because it has been marked as rollback-only"); } return; } processCommit(defStatus); }
/** * This implementation of rollback handles participating in existing * transactions. Delegates to {@code doRollback} and * {@code doSetRollbackOnly}. * @see #doRollback * @see #doSetRollbackOnly */ public final void rollback(TransactionStatus status) throws TransactionException { if (status.isCompleted()) { throw new IllegalTransactionStateException( "Transaction is already completed - do not call commit or rollback more than once per transaction"); } DefaultTransactionStatus defStatus = (DefaultTransactionStatus) status; processRollback(defStatus); }
/** * PROPAGATION_MANDATORY:强制性的事务,当前的事务上下文中不存在事务的话,会抛出 {@link IllegalTransactionStateException} */ @Test(expected = IllegalTransactionStateException.class) public void testTxRollbackInnerTxRollbackPropagationMandatory() { springTxTest.txRollbackInnerTxRollbackPropagationMandatory(); }
/** * PROPAGATION_NEVER:不允许当前事务上下文中存在事务,如果有,则抛出 {@link IllegalTransactionStateException} */ @Test(expected = IllegalTransactionStateException.class) public void testTxRollbackInnerTxRollbackPropagationNever2() { springTxTest.txRollbackInnerTxRollbackPropagationNever2(); }
@Test(expected = IllegalTransactionStateException.class) public void testRetryFailsOnTransaction() { itemService.saveItem(); }
/** * Set the given transaction rollback-only. Only called on rollback * if the current transaction participates in an existing one. * <p>The default implementation throws an IllegalTransactionStateException, * assuming that participating in existing transactions is generally not * supported. Subclasses are of course encouraged to provide such support. * @param status the status representation of the transaction * @throws TransactionException in case of system errors */ protected void doSetRollbackOnly(DefaultTransactionStatus status) throws TransactionException { throw new IllegalTransactionStateException( "Participating in existing transactions is not supported - when 'isExistingTransaction' " + "returns true, appropriate 'doSetRollbackOnly' behavior must be provided"); }