This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

are stateless beans thread safe  RSS feed

 
satish bodas
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, EJBs are threadsafe.
 
satish bodas
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Paul
 
Scott Selikoff
author
Bartender
Posts: 4093
21
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mike Po
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!