要求在REQUIRES_NEW内的REQUIRES_NEW内......等等 [英] REQUIRES_NEW within REQUIRES_NEW within REQUIRES_NEW ... on and on

查看:163
本文介绍了要求在REQUIRES_NEW内的REQUIRES_NEW内......等等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JBoss 4.x

EJB 3.0

JBoss 4.x
EJB 3.0

我见过以下代码(大大缩写):

I've seen code like the following (greatly abbreviated):

@Stateless
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
public class EJB1 implements IEJB1
{
   @EJB
   private IEJB1 self;

   @EJB 
   private IEJB2 ejb2;

   @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
   public boolean someMethod1()
   {
     return someMethod2();
   }

   @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
   public boolean someMethod2()
   {
     return self.someMethod3();
   }

   @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
   public boolean someMethod3()
   {
     return ejb2.someMethod1();
   }
}

并说EJB2几乎是EJB1的精确副本(相同的三种方法)和 EJB2.someMethod3()调用 EJB3.someMethod1(),然后最后在 EJB3.someMethod3()写入数据库。

And say EJB2 is almost an exact copy of EJB1 (same three methods), and EJB2.someMethod3() calls into EJB3.someMethod1(), which then finally in EJB3.someMethod3() writes to the DB.

这是一个人为的例子,但看到类似的代码在我们的代码库中。代码实际上运行得很好。

This is a contrived example, but have seen similar code to the above in our codebase. The code actually works just fine.

然而,这感觉很糟糕,我担心 @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)在每个甚至不实际执行任何数据库写操作的方法上。这是否实际上每次为每个方法调用创建一个新事务,结果为:

However, it feels like terrible practice and I'm concerned about the @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) on every method that doesn't even actually perform any DB writes. Does this actually create a new transaction every single time for every method call with the result of:

新事务

-new transaction

- 新交易

---新交易

...(更多)

------- new transaciton( DB写)

new transaction
-new transaction
--new transaction
---new transaction
...(many more)
-------new transaciton (DB write)

然后在那时打开?这会不会引起性能问题?其他想法?

And then unwraps at that point? Would this ever be a cause for performance concern? Additional thoughts?

推荐答案


这实际上每次为
创建一个新交易每个方法调用

Does this actually create a new transaction every single time for every method call

不,它没有。只有在通过另一个bean的EJB引用调用方法时,才会创建新事务。在同一个bean中从 method1 调用 method2 将不会产生新事务。

No, it doesn't. The new transaction will be created only when calling method by EJB reference from another bean. Invoking method2 from method1 within the same bean won't spawn the new transaction.

另请参阅这里这里。后者是非常好的文章,解释了EJB中的事务管理。

See also here and here. The latter is exceptionally good article, explaining transaction management in EJB.

编辑:

感谢@korifey指出,方法2实际上在bean引用上调用method3,从而导致一笔新交易。


Thanks @korifey for pointing out, that method2 actually calls method3 on bean reference, thus resulting in a new transaction.

这篇关于要求在REQUIRES_NEW内的REQUIRES_NEW内......等等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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