整个班级的交易注释+不包括单一方法 [英] Transactional annotation on whole class + excluding a single method

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

问题描述

我有一个有 @Transactional 注解的类(而不是标记它的所有方法)。

虽然我在该类中有一个单独的方法,但不应注释为 @Transactional



我的问题是否有一个注释可以用这种方法将其标记为非事务性?或者我应该开始标记每个单一的方法在这个类作为交易排除这种方法(很多工作)$ / b>
$ b

谢谢。

解决方案

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

  / ** 
*执行非事务性操作,挂起当前事务if一个存在。
*类似于同名的EJB事务属性。
*< p>注意:在所有事务管理器上,实际的事务处理暂停不适用于开箱即用的
*。这特别适用于JtaTransactionManager,
*需要javax.transaction.TransactionManager}为
*使其可用(它在标准J2EE中是特定于服务器的)。
* see org.springframework.transaction.jta.JtaTransactionManager#setTransactionManager
* /
NOT_SUPPORTED(TransactionDefinition.PROPAGATION_NOT_SUPPORTED),

/ **
*非事务性执行,如果事务存在则抛出异常。
*类似于同名的EJB事务属性。
* /
NEVER(TransactionDefinition.PROPAGATION_NEVER),//也许不是这个



<

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

你可以找到所有的传播策略 here


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

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)

thanks.

解决方案

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天全站免登陆