简体   繁体   English

为什么EJB无状态会话bean一定不能实现SessionSynchronization?

[英]Why EJB stateless session bean must not implement SessionSynchronization?

I'm precisely talking about EJB 3.1, and I know that per specifications a stateless session bean must not implement the SessionSynchronization interface, but could someone explain to me why ? 我只是在谈论EJB 3.1,我知道按照规范,无状态会话bean一定不能实现SessionSynchronization接口,但是有人可以向我解释为什么吗? So, I am not asking for a workaround but I would like to know the reasons behind this restristion. 因此,我并不是要寻求解决方法,但我想知道这种限制的原因。

UPDATE: 更新:

I don't think this is related to transaction boundaries, because the container should commit when the business method has completed, as per section 13.6.2.2: 我认为这与事务边界无关,因为根据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. 在将方法结果发送到客户端之前,容器执行提交协议。

And per The Java EE 6 Tutorial : 并且根据Java EE 6教程

Typically, the container begins a transaction immediately before an enterprise bean method starts and commits the transaction just before the method exits. 通常,容器在企业bean方法启动之前立即开始事务,并在方法退出之前提交事务。 Each method can be associated with a single transaction. 每种方法都可以与单个事务关联。 Nested or multiple transactions are not allowed within a method. 方法中不允许嵌套或多个事务。

The question is related to transactions topic. 该问题与交易主题有关。 Because stateless session beans (SLSB) do not keep conversational state between subsequent requests then any changes in resource state (eg database update) are local in the context of method. 由于无状态会话Bean(SLSB)不会在后续请求之间保持会话状态,因此资源状态的任何更改(例如数据库更新)在方法上下文中都是本地的。 Transaction cannot be spanned across multiple methods for SLSB. 不能跨SLSB的多种方法处理事务。
In regard to stateful session bean situation (SFSB) is a bit different. 关于有状态会话bean情况(SFSB)有所不同。 This is not mandatory to complete transaction before method ends. 这对于在方法结束之前完成事务不是强制性的。 Therefore transaction can span multiple methods. 因此,事务可以跨越多种方法。
When you let the container to manage transactions (CMT) and use SFSB there is possibility that you do not know precisely when transaction is started/completed. 当您让容器管理事务(CMT)并使用SFSB时,您可能无法确切知道何时开始/完成事务。 You need a mechanism that notifies when those actions happen. 您需要一种机制来通知这些操作何时发生。 And this is SessionSynchronization interface implementation purpose. 这就是SessionSynchronization接口的实现目的。
SLBSs with CMT do not receive transaction start/end notification because transaction boundaries are known and they are limited to a business method execution. 具有CMT的SLBS不接收事务开始/结束通知,因为事务边界是已知的,并且它们仅限于业务方法执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM