我有一个带有 @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
所以用其中任何一个来注释你的类中的方法。
@Transactional
public class MyTransactionalClass {
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public void nonTransactionalMethod() {...}
}
你可以找到所有的传播策略here .
关于spring - 整个类的事务注释+不包括单个方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18233171/