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

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

问题描述

方法B在(新的)事务中运行?



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

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

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

EJB是容器管理的,在require_new事务中使用methodB ,以及所需交易中的方法A.因此:

 < container-transaction id =MethodTransaction_1178709616940> 
< method id =MethodElement_1178709616955>
< ejb-name> MyName< / ejb-name>
< method-name> *< / method-name>
< trans-attribute>必需< / 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调用方法A调用EJB方法。
methodA现在在一个事务中运行。将来自methodA的methodB的后续调用在同一事务中运行,还是在新事务中运行?
(记住,这里是实际的代码)没有明确的ejb调用方法B)

解决方案

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


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

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();
    }
}

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>

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)

解决方案

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