整个类的事务注释+排除单个方法
我有一个带有@Transactional注释的类(而不是对其所有方法进行标记)。
虽然我在该类中只有一个方法,但不应将其注释为@Transactional。
我的问题是我可以在此方法中添加注释以将其标记为“非事务性”吗?或者我应该开始将此类中的每个方法都标记为“事务性”,但不包括此方法(很多工作)
谢谢。
回答:
有不同的事务传播策略可以使用。这些存在于枚举中Propagation。您可能要使用的是
/** * Execute non-transactionally, suspend the current transaction if one exists.
 * Analogous to EJB transaction attribute of the same name.
 * <p>Note: Actual transaction suspension will not work on out-of-the-box
 * on all transaction managers. This in particular applies to JtaTransactionManager,
 * which requires the {@code javax.transaction.TransactionManager} to be
 * made available it to it (which is server-specific in standard J2EE).
 * @see org.springframework.transaction.jta.JtaTransactionManager#setTransactionManager
 */
NOT_SUPPORTED(TransactionDefinition.PROPAGATION_NOT_SUPPORTED),
/**
 * Execute non-transactionally, throw an exception if a transaction exists.
 * Analogous to EJB transaction attribute of the same name.
 */
NEVER(TransactionDefinition.PROPAGATION_NEVER), // maybe not this one
因此,使用这两种方法在您的类中注释方法。
@Transactionalpublic class MyTransactionalClass { 
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public void nonTransactionalMethod() {...}
}
您可以在此处找到所有传播策略。
以上是 整个类的事务注释+排除单个方法 的全部内容, 来源链接: utcz.com/qa/432267.html

