This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
The SessionSynchronization interface, which is optional, allows stateful session bean instances to receive transaction synchronization notifications. For example, you could synchronize the instance variables of an enterprise bean with their corresponding values in the database.
Do stateless or singleton beans need to synchronize with the transaction before the method ,in which a transaction begins, completes?
But it seems to me that at the end of the thread, people are still wondering why stateless or singleton cannot implement SessionSynchronization interface?
Joined: Jul 29, 2012
I may be able to answer my own question.
On Frit's notes p.49,
A stateful session bean instance is not required to commit a started transaction before a business method or interceptor method returns.
A stateless session bean instance must commit a transaction before a business method or interceptor method returns....
A singleton session bean instance must commit a transaction before a business method or interceptor method returns....
I guess a stateless / singleton session bean commits a transaction before the business method completes and they don't need to be notified by the container under these conditions:
1. afterBegin - the container notifies the bean when a new transaction starts.
2. afterCompletion- the container notifies the bean when transaction commit protocol completes.
3. beforeCompletion - the container notifies the bean when the transaction is about to commit.
Since stateless or singleton session bean must commit and complete a transaction right before the business method finishes, the container does not need to notify the bean about the status of the transaction. It is because when the business method finishes, it guarantees that a transaction is committed and completes.
However, a stateful session bean may not commit a transaction before a business method finishes, this stateful bean may invoke some subsequent business methods afterwards to continue some sub-tasks within the same transaction. The transaction may or may not be committed or completed after a sequence of business method invocations. That is why the stateful bean needs to be notified about the status of the transaction during multiple business method executions. For example, the bean may need to know if the transaction has been committed after the third business method finishes.
Correct me if I am wrong.
subject: Why SessionSynchronization is only for stateful session bean?