整个类的事务注释 + 不包括单个方法 [英] Transactional annotation on whole class + excluding a single method

查看:28
本文介绍了整个类的事务注释 + 不包括单个方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 @Transactional 注释的类(而不是为它的所有方法标记它).

I have a class with @Transactional annotation (instead of marking it for all of its method).

虽然我在该类中有一个不应该被注释为 @Transactional 的方法.

Although i have a single method inside that class that shouldn't be annotated as @Transactional.

我的问题是是否可以在此方法中添加注释以将其标记为非事务性"?或者我应该开始将此类中的每个方法标记为事务性",不包括此方法(大量工作)

My question is is there an annotation i can put in this method to mark it as "non-transactional"? or should i start marking each single method in this class as "transactional" excluding this method (a lot of work)

谢谢.

推荐答案

可以使用不同的事务传播策略.这些存在于枚举 Propagation 中.您可能想要使用的是

There are different transaction propagation strategies to use. These exist in the enum Propagation. The ones you might want to use are

/**
 * 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

因此,请使用其中任何一个注释您的类中的方法.

So annotate the method inside your class with either of these.

@Transactional
public class MyTransactionalClass { 
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public void nonTransactionalMethod() {...}
}

你可以找到所有的传播策略此处.

You can find all the propagation strategies here.

这篇关于整个类的事务注释 + 不包括单个方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆