0
votes

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

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:

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.

1

1 Answers

3
votes

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 (e.g. database update) are local in the context of method. Transaction cannot be spanned across multiple methods for SLSB.
In regard to stateful session bean situation (SFSB) is a bit different. 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. You need a mechanism that notifies when those actions happen. And this is SessionSynchronization interface implementation purpose.
SLBSs with CMT do not receive transaction start/end notification because transaction boundaries are known and they are limited to a business method execution.