permaculture playing cards
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes SessionSynchronization interface 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 "SessionSynchronization interface" Watch "SessionSynchronization interface" New topic

SessionSynchronization interface

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 10032


Why do we need to (optionally) implement SessionSynchronization interface for Stateful Session beans?

SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Celinio Fernandes
Ranch Hand

Joined: Jun 28, 2003
Posts: 549

I think you should rephrase your question:
the "need" and "optionally" words don't go well together.
Anyways, here's your answer:

A stateful session bean class can optionally implement the javax.ejb.SessionSynchronization
interface. This interface provides the session bean instances with transaction synchronization
notifications. The instances can use these notifications, for example, to manage database data they may
cache within transactions´┐Że.g., if the Java Persistence API is not used.

SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCBCD 5
Visit my blog
Nitin Takale

Joined: Nov 30, 2006
Posts: 26
Hi Jothi,

Some times you write a stateful Session Bean which does some database operations like.. Insert update and delete and for these operations you have written some methods in your bean class say.. goInsert(), goUpdate(),goDelete().

Since it is stateful session bean for your business logic you'll declare some instance variables in your class and those are nothing but the database fields, values of which you are going to take as parameters of these methods (goInsert,goUpdate,goDelete). It may have one more method called getData(id) which selects the data populates it inside the bean variables.

For this bean container is managing the transaction.

Consider that For the first time when client wants do to some processing it selects the data. so it calls getData(id), all the variables will be populated with data from database. Data has given to client through some DTO. Client modifies that data and calls ....

... goUpdate by passing changed values to it.. the container first will start a new transaction then it'll invoke the method. After the method call container is going to commit.

During this method invocation suppose you've updated few values in database and something goes wrong at runtime and exception occurs... so what will happen to the data inside the database and to the values of instance variables

to handle such situations... the bean has to implement SessionSynchronization interface where some callbacks will get called before start of Tx after end of Tx etc...

just correlate this example after applying SessionSynchronization and follow the specs

I hope this will clear your doubt.




Nitin<br /> <br />SCJP 6.0<br />SCBCD 5.0
Celinio Fernandes
Ranch Hand

Joined: Jun 28, 2003
Posts: 549

Only stateful session beans with container managed transaction demarcation can implement this interface.
Session beans using BMT must not implement SessionSynchronization.
I agree. Here's the link:
subject: SessionSynchronization interface
It's not a secret anymore!