Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why stateless session beans are single threaded?

Tags:

java

ejb

As per my understanding stateless session beans are used to code the business logic. They can not store data in their instance variables because their instance is shared by multiple requests. So they seem to be more like Singleton classes. However the difference is contain creates (or reuses from pool) the separate instance of stateless session beans for every request.

After googling I could find the reasoning that the Java EE specification says they are suppose to be single threaded. But I can't get the reason why the are specified to be SINGLE THREADED?

like image 979
jatanp Avatar asked Aug 27 '08 08:08

jatanp


People also ask

Is stateless bean singleton?

Singletons offer similar functionality to stateless session beans but differ from them in that there is only one singleton session bean per application, as opposed to a pool of stateless session beans, any of which may respond to a client request.

Are stateless beans thread safe?

So a Stateless seession bean can "serve" at most one request at a time, this is usually implemented by the container managing a pool of beans. The great goal of all this is Thread safety.

Are stateless EJB thread safe?

Working with EJBs without any further configuration is thread-safe regardless whether you are invoking one method or multiple methods concurrently.

What is the reason behind a stateful session bean instead of a stateless session bean in EJB?

The main difference between Stateless and Stateful Session Bean is that Stateless Session Bean is a business object without state (data) that describes the business logic while Stateful Session Bean is a business object with a state (data) that describes the business logic. EJB stands for Enterprise Java Bean.


2 Answers

The SLSBs are single threaded because of the TX Context, Principal is associated with a bean instance when it is called. These beans are pooled and unless the max pool size is reached are processed in separate threads ( Vendor dependent).

If SLSBs were designed thread safe every call would have looked like a servlet doGet/Post with request info containing Tx Context , Security Context info and etc. So at least the code looks clean (developer dependent).

like image 156
Alexander Stolz Avatar answered Sep 28 '22 19:09

Alexander Stolz


The primary reason stateless session beans are single threaded is to make them highly scalable for the container. The container can make a lot of simplifying assumptions about the runtime environment. A second reason is to make life easier for the developer because the developer doesn't have to worry about any synchronization or re-entrancy in his business logic because the bean will never be called in another thread context.

I remember the reasoning being discussed in the reviews of the original EJB 1.0 specification. I would look at the goals section of the specification. See http://java.sun.com/products/ejb/docs.html for the list of specifications.

like image 21
David G Avatar answered Sep 28 '22 20:09

David G