File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Why SessionSynchronization is only for stateful session bean? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "Why SessionSynchronization is only for stateful session bean?" Watch "Why SessionSynchronization is only for stateful session bean?" New topic
Author

Why SessionSynchronization is only for stateful session bean?

Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 813
    
    1
In J2EE tutorial , it says

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?

This topic was discussed here http://www.coderanch.com/t/161691/java-EJB-SCBCD/certification/SessionSynchronization-interface-stateless-session-bean

But it seems to me that at the end of the thread, people are still wondering why stateless or singleton cannot implement SessionSynchronization interface?
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 813
    
    1
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why SessionSynchronization is only for stateful session bean?