gpt4 book ai didi

java - 为什么 EJB 无状态 session bean 不能实现 SessionSynchronization?

转载 作者:行者123 更新时间:2023-12-01 21:54:13 25 4
gpt4 key购买 nike

我正在谈论 EJB 3.1,并且我知道根据规范,无状态 session bean 不得实现 SessionSynchronization 接口(interface),但有人可以向我解释为什么吗?因此,我并不是在寻求解决方法,而是想知道这种限制背后的原因。

更新:

我认为这与事务边界无关,因为容器应该在业务方法完成时提交,根据第 13.6.2.2 节:

The container attempts to commit the transaction when the business method has completed. The container performs the commit protocol before the method result is sent to the client.

按照The Java EE 6 Tutorial :

Typically, the container begins a transaction immediately before an enterprise bean method starts and commits the transaction just before the method exits. Each method can be associated with a single transaction. Nested or multiple transactions are not allowed within a method.

最佳答案

该问题与交易主题相关。由于无状态 session Bean (SLSB) 不保留后续请求之间的 session 状态,因此资源状态的任何更改(例如数据库更新)在方法的上下文中都是本地的。对于 SLSB,事务不能跨越多种方法。
关于有状态 session bean 的情况 (SFSB) 有点不同。在方法结束之前完成事务并不强制要求。因此事务可以跨越多种方法。
当您让容器管理事务(CMT)并使用 SFSB 时,您可能无法准确知道事务何时开始/完成。您需要一种机制来通知这些操作何时发生。这就是SessionSynchronization接口(interface)实现的目的。
具有 CMT 的 SLBS 不会收到事务开始/结束通知,因为事务边界已知,并且它们仅限于业务方法执行。

关于java - 为什么 EJB 无状态 session bean 不能实现 SessionSynchronization?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34645580/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com