• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why SessionSynchronization is only for stateful session bean?

 
Bartender
Posts: 2419
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 https://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
Bartender
Posts: 2419
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Put a gun against his head, pulled my trigger, now he's dead, that tiny ad sure bled
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic