EJB 请求对其自身的引用?
如果 EJB 的方法对同一 EJB 进行 JNDI 查找,然后执行其他一些方法,是否可以保证在同一线程中执行? EJB对象的实际实例怎么样?它与调用方法中使用的实例是否相同?
If a method of an EJB makes a JNDI lookup to the same EJB and then executes some other method, is this guaranteed to be executed in the same thread? How about the actual instance of the EJB object; is it the same instance as the one used in the calling method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
EJB 规范对于将 EJB 注入自身的可能性没有限制(请参阅 EJB 3.1 规范,第 16.5.1.1 章):
与每个 EJB 调用一样,它将始终在同一个线程中执行;根据不可重入实例规则,它不能是同一个实例(第 4.10.13 章):
这意味着:如果 bean A 调用 bean B 上的方法,并且 B 调用 A 上的任何方法,则容器必须确保 B 调用 A 的另一个实例;事实上,它是同一个线程并不重要,因为可重入和线程安全是不同的事情。
当 A 通过查找的 JNDI 引用调用自身时,也适用相同的情况,因为容器在运行时参与查找 A 的自由实例。相反,如果 A 直接调用其方法之一,则这是对“的简单 Java 方法调用” this”,不涉及容器。
The EJB specification makes no restriction as to the possibility of injecting an EJB into itself (see EJB 3.1 spec, chapter 16.5.1.1):
As with every EJB call, it will always be executed in the same thread; based on the Non-reentrant instances rule, it must not be the same instance (chapter 4.10.13):
This means: if bean A invokes a method on bean B, and B invokes any method on A, the container has to make sure that B calls another instance of A; the fact that it's the same thread doesn't matter, as reentrancy and thread-safety are different things.
The same situation applies when A calls itself through a looked-up JNDI reference, as container is involved at runtime in looking for a free instance of A. Conversely, if A directly invokes one of its methods, it's a simple Java method invocation on "this", not involving the container.