Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

stateless Session Bean Thread safety issue  RSS feed

 
Apps Yadav
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everybody,
In stateless session bean state of an object is not maintained across the method call, so why they are inherently thread safe.(As per J2EE specification)
Thanks in advance.

Aparna.
 
Valentin Tanase
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Aparna,

The meaning of thread safe, for all type of beans actually, makes references to bean�s capability of synchronizing its attributes in a multithreaded environment. Just for a moment imagine the danger of declaring global variables in servlets. Some clients read the attribute, some of them will modify it. With servlets the client needs to provide synchronized access to servlet�s attributes, in order to avoid data corruption; this is one good reason why global variable are avoided with servlets. EJBs completely overcome this issue, by pooling the beans and having each client talking to a specific instance. Therefore when two clients concurrently invoke the same method that supposes to change an instance variable, there is no danger to get corrupted data, because each client holds a different bean instance.
Regards.
 
Shrinivas Mujumdar
Ranch Hand
Posts: 328
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
What you are saying is true but the question still remains if stateless session bean do not have any data member then where the question of data corruption occurs as well as the instances are seperate for each client so again the probability of race around condition is very less.so why they are thread safe by default.


Regards
Shrinivas
 
Valentin Tanase
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's not always the case actually. SLSB could have instance variables like any other java objects. One example might be a SLSB that uses a DAO class for updating the database. One way to design the bean is to use an attribute that maps to the DAO and initialize the DAO in ejbCreate(), like this:

And because the SLSB is thread safe, the developers don�t need to bother whether the MyDAO class is design thread safe or not (the story would be different if the SLSB would be replaced with a servlet). However they cannot rely on the internal state of the instance variables, because they might talk to different bean instance next time.
Regards.
 
Bhanu
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Valentine,
It was a very good example that you choosen for explanation. Thanks for enlightening us.
 
Valentin Tanase
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're very welcome Bhanu
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!