Java 类org.springframework.transaction.IllegalTransactionStateException 实例源码

项目:spring4-understanding    文件:WebSphereUowTransactionManagerTests.java   
@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
    }
}
项目:spring4-understanding    文件:WebSphereUowTransactionManagerTests.java   
@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
    }
}
项目:class-guard    文件:WebSphereUowTransactionManagerTests.java   
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
    }
}
项目:class-guard    文件:WebSphereUowTransactionManagerTests.java   
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
    }
}
项目:lams    文件:AbstractPlatformTransactionManager.java   
/**
 * 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);
}
项目:lams    文件:AbstractPlatformTransactionManager.java   
/**
 * 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);
}
项目:gorm-redis    文件:RedisTransaction.java   
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());
    }
}
项目:spring4-understanding    文件:AbstractPlatformTransactionManager.java   
/**
 * 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);
}
项目:spring4-understanding    文件:AbstractPlatformTransactionManager.java   
/**
 * 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);
}
项目:invesdwin-context-persistence    文件:SimpleTestDaoTest.java   
@Test
public void testFlushWithoutTransaction() {
    try {
        new TransactionalAspectMethods().testFlushWithoutTransaction();
        Assertions.failExceptionExpected();
    } catch (final Throwable t) {
        Assertions.assertThat(t).isInstanceOf(IllegalTransactionStateException.class);
    }
}
项目:invesdwin-context-persistence    文件:SimpleTestDaoTest.java   
@Test
public void testFlushWithoutTransaction() {
    Assertions.assertThrows(IllegalTransactionStateException.class, new Executable() {
        @Override
        public void execute() throws Throwable {
            new TransactionalAspectMethods().testFlushWithoutTransaction();
        }
    });
}
项目:invesdwin-context-persistence    文件:TestDaoTest.java   
@Test
public void testFlushWithoutTransaction() {
    Assertions.assertThrows(IllegalTransactionStateException.class, new Executable() {
        @Override
        public void execute() throws Throwable {
            new TransactionalAspectMethods().testFlushWithoutTransaction();
        }
    });
}
项目:invesdwin-context-persistence    文件:SimpleTestDaoTest.java   
@Test
public void testFlushWithoutTransaction() {
    try {
        new TransactionalAspectMethods().testFlushWithoutTransaction();
        Assertions.failExceptionExpected();
    } catch (final Throwable t) {
        Assertions.assertThat(t).isInstanceOf(IllegalTransactionStateException.class);
    }
}
项目:training    文件:TransactionUtilTest.java   
@Test(expected = IllegalTransactionStateException.class)
public void testMandatoryFailsWhenInvokedWithoutTx() {
    txUtil.executeWith_MANDATORY(new Runnable() {
        @Override
        public void run() {
            fail("Must never reach this point!");
        }
    });
}
项目:ignite    文件:GridSpringTransactionManagerSelfTest.java   
/** */
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());
}
项目:db-util    文件:OptimisticConcurrencyControlAspect.java   
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);
}
项目:db-util    文件:OptimisticConcurrencyControlAspectTest.java   
@Test(expected = IllegalTransactionStateException.class)
public void testRetryFailsOnTransaction() {
    try {
        TransactionSynchronizationManager.setActualTransactionActive(true);
        itemService.saveItem();
    } finally {
        TransactionSynchronizationManager.setActualTransactionActive(false);
    }
}
项目:class-guard    文件:AbstractPlatformTransactionManager.java   
/**
 * 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);
}
项目:class-guard    文件:AbstractPlatformTransactionManager.java   
/**
 * 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);
}
项目:spring-tx-test    文件:TestMain.java   
/**
 * PROPAGATION_MANDATORY:强制性的事务,当前的事务上下文中不存在事务的话,会抛出 {@link IllegalTransactionStateException}
 */
@Test(expected = IllegalTransactionStateException.class)
public void testTxRollbackInnerTxRollbackPropagationMandatory() {
    springTxTest.txRollbackInnerTxRollbackPropagationMandatory();
}
项目:spring-tx-test    文件:TestMain.java   
/**
 * PROPAGATION_NEVER:不允许当前事务上下文中存在事务,如果有,则抛出 {@link IllegalTransactionStateException}
 */
@Test(expected = IllegalTransactionStateException.class)
public void testTxRollbackInnerTxRollbackPropagationNever2() {
    springTxTest.txRollbackInnerTxRollbackPropagationNever2();
}
项目:vladmihalcea.wordpress.com    文件:OptimisticLockingTest.java   
@Test(expected = IllegalTransactionStateException.class)
public void testRetryFailsOnTransaction() {
    itemService.saveItem();
}
项目:lams    文件:AbstractPlatformTransactionManager.java   
/**
 * 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");
}
项目:spring4-understanding    文件:AbstractPlatformTransactionManager.java   
/**
 * 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");
}
项目:class-guard    文件:AbstractPlatformTransactionManager.java   
/**
 * 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");
}