我有一个带有@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。