This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes EJB and other Java EE Technologies and the fly likes are stateless beans thread safe Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Reply Bookmark "are stateless beans thread safe " Watch "are stateless beans thread safe " New topic
Author

are stateless beans thread safe

satish bodas
Ranch Hand

Joined: Jun 19, 2008
Posts: 116
I am being lazy here - but as I have started reading about EJB some questions come up .

So before I lose track of what doubts I have - I thought would place them here .

So this is an excuse in advance if some one tells me that I am asking them to do my homework !

Are stateless beans threadsafe ?
How does the container go about it ? after the bean is picked from pool - can container use same bean to process multiple requests ?

I am assuming that stateful beans ( since they are per client ) would remain available specific to a client and thereby there cannot be any issues regarding thread safety in case of steful session beans ?

Thanks ,
~satish
[ August 01, 2008: Message edited by: satish bodas ]
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Yes, EJBs are threadsafe.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
satish bodas
Ranch Hand

Joined: Jun 19, 2008
Posts: 116
Thanks Paul
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3652

I agree with Paul although I think you can make them not thread safe by spawning your own threads, granted in J2EE spawning threads is a really bad idea.


My Blog: Down Home Country Coding with Scott Selikoff
Mike Po
Greenhorn

Joined: Nov 19, 2006
Posts: 4
What is a thread-saftiness? If an object is shielded by another object( proxy or container) which serializes concurrent threads, does it make the first object itself thread-safe?

Why in EJB 1.1. and EJB 2.0 it was always highly recommended not to share reference to an EJB among client's threads?

It is used to belive that Entity Beans are thread-safe. Hwo is controlling concurrent access to independent Load and Store methonds? To me, if it is a container, then the Entity Bean itself is NOT thread safe.

In general, I think that from the performace persective, it makes sense to have EJB not thread safe becuase EJB Container can do this job for EJBs. I need to look into EJB 3.0 - how POJO resolves this issue.
Per Lindberg
Ranch Hand

Joined: Jan 17, 2008
Posts: 48
Scott Selikoff wrote:I agree with Paul although I think you can make them not thread safe by spawning your own threads, granted in J2EE spawning threads is a really bad idea.


Does that include the case where you inject a stateless EJB in a ServletContextListener with

@EJB FooLocal fooLocal;

and pass it as a parameter to a spawned thread (which has to do some ansynchronous tasks):

MaintencanceThread maintencanceThread = new MaintenanceThread(fooLocal);
t = new Thread(maintenanceThread);
t.setDaemon(false);
t.start();


Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

You are not spawning a thread from the EJB there. That is the part that is specifically precluded by the EJB specification.
Per Lindberg
Ranch Hand

Joined: Jan 17, 2008
Posts: 48
Ok, Paul, that's right.

But now suppose that this maintenance thread listens to a ServerSocket for TCP connect requests, and then spawns new threads (using an Executor). Now these threads would all use the same instance of the stateless session bean. That wouldn't be thread-safe, right?

 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: are stateless beans thread safe
 
Similar Threads
Bean Scalability!!!
Question on exercise pg 108 HFE
Stateless beans and SessionSynchronization
Stateless session bean!!!
Stateless session beans and Transactions