Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why pool Stateless session beans?

People also ask

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

Stateful session beans maintain the state associated with a client. Each stateful session bean serves exactly one client. Stateless session beans are intended to be simple and lightweight; that is, they are easy to develop with low runtime resource requirements on the server.

What is stateless session bean?

A stateless session bean does not maintain a conversational state with the client. When a client invokes the methods of a stateless bean, the bean's instance variables may contain a state specific to that client but only for the duration of the invocation.

What is bean instance pooling?

Instance pooling allows EJB Server to maintain a cache of component instances and bind them to client sessions on an as-needed basis. When components support instance pooling, the scalability of your application increases.

Should spring beans be stateless?

Basically, a well written spring bean should be stateless with only dependency injected, thread local, instance variables. With that in mind, there's really no "global state" or shared state to speak of.


Pooling does several things.

One, by having one bean per instance, you're guaranteed to be threads safe (Servlets, for example, are not thread safe).

Two, you reduce any potential startup time that a bean might have. While Session Beans are "stateless", they only need to be stateless with regards to the client. For example, in EJB, you can inject several server resources in to a Session Bean. That state is private to the bean, but there's no reason you can't keep it from invocation to invocation. So, by pooling beans you reduce these lookups to only happening when the bean is created.

Three, you can use bean pool as a means to throttle traffic. If you only have 10 Beans in a pool, you're only going to get at most 10 requests working simultaneously, the rest will be queued up.


Pooling enhances performance.

A single instance handling all requests/threads would lead to a lot of contention and blocking.

Since you don't know which instance will be used (and several threads could use a single instance concurrently), the beans must be threadsafe.

The container can manage pool size based on actual activity.


The transactionality of the Java EE model uses the thread context to manage the transaction lifecycle.

This simplification exists so that it is not necessary to implement any specific interface to interact with the UserTransaction object directly; when the transaction is retrieved from the InitialContext (or injected into the session bean) it is bound to a thread-local variable for reuse (for example if a method in your stateless session bean calls another stateless session bean that also uses an injected transaction.)