EJB 请求对其自身的引用?

发布于 2024-12-25 01:34:28 字数 94 浏览 0 评论 0原文

如果 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

内心荒芜 2025-01-01 01:34:28

EJB 规范对于将 EJB 注入自身的可能性没有限制(请参阅 EJB 3.1 规范,第 16.5.1.1 章):

[@EJB] 引用可以是会话 Bean 的业务接口,也可以是会话 Bean 的无接口视图,也可以是会话 Bean 或实体 Bean 的本地 Home 接口或远程 Home 接口。

与每个 EJB 调用一样,它将始终在同一个线程中执行;根据不可重入实例规则,它不能是同一个实例(第 4.10.13 章):

[...] 有状态和无状态会话 Bean 不必编码为可重入。此规则的一个含义是应用程序无法对无状态或有状态会话 Bean 实例进行环回调用。

这意味着:如果 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):

The [@EJB] reference may be to a session bean’s business interface or to a session bean’s no-interface view or to the local home interface or remote home interface of a session bean or entity bean.

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):

[...] Stateful and stateless session beans do not have to be coded as reentrant. One implication of this rule is that an application cannot make loopback calls to a stateless or stateful session bean instance.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文