本地方法调用中的 EJB 事务 [英] EJB Transactions in local method-calls

查看:19
本文介绍了本地方法调用中的 EJB 事务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下设置中,方法 B 是否在(新)事务中运行?

In the following setup, does method B run in a (new) transaction?

一个 EJB,有两个方法,方法 A 和方法 B

An EJB, having two methods, method A and method B

public class MyEJB implements SessionBean
    public void methodA() {
       doImportantStuff();
       methodB();
       doMoreImportantStuff();
    }

    public void methodB() {
       doDatabaseThing();
    }
}

EJB 是容器管理的,方法 B 在 requires_new 事务中,方法 A 在 required 事务中.因此:

The EJB is container managed, with methodB in requires_new transaction, and method A in required transaction. thus:

<container-transaction id="MethodTransaction_1178709616940">
  <method id="MethodElement_1178709616955">
    <ejb-name>MyName</ejb-name>
    <method-name>*</method-name>
  <trans-attribute>Required</trans-attribute>
  </method>
  <method id="MethodElement_1178709616971">
    <ejb-name>MyName</ejb-name>
    <method-name>methodB</method-name>
  </method>
  <trans-attribute>RequiresNew</trans-attribute>
</container-transaction>

现在让另一个 EJB 使用 EJB 方法调用来调用 methodA.methodA 现在在事务中运行.从methodA 对methodB 的后续调用是在同一个事务中运行,还是在一个新事务中运行?(注意,这是这里的实际代码.没有对方法 B 的显式 ejb 调用)

Now let another EJB call methodA with an EJB method call. methodA now runs in an transaction. Will the subsequent call to methodB from methodA run in the same transaction, or does it run in a new transaction? (mind, it's the actual code here. There is no explicit ejb-call to method B)

推荐答案

你对 methodB() 的调用是一个普通的方法调用,不被 EJB 容器拦截;在运行时,EJB 容器将注入一个 proxy 而不是您的类的实例,这是它在调用您的方法之前拦截调用和设置环境的方式.如果您使用 this,您将直接调用一个方法,而不是通过代理.因此,无论 ejb-jar.xml 中为通过 EJB 接口的调用定义了什么,这两种方法都将使用相同的事务.

Your call to methodB() is an ordinary call of a method, not intercepted by the EJB container; at run-time the EJB container will inject a proxy and not an instance of your class, this is the way it intercepts calls and setup the environment before calling your method. If you use this you're calling a method directly and not through the proxy. Hence both methods will use the same transaction, regardless to what is defined in ejb-jar.xml for calls through EJB interfaces.

这篇关于本地方法调用中的 EJB 事务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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