Why pool Stateless session beans?

后端 未结 5 605
半阙折子戏
半阙折子戏 2020-12-02 19:04

Stateless beans in Java do not keep their state between two calls from the client. So in a nutshell we might consider them as objects with business methods. Each method take

相关标签:
5条回答
  • 2020-12-02 19:20

    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.

    0 讨论(0)
  • 2020-12-02 19:33

    Life cycle of the Statelesss session beans are Doesnot exist, Passive and MethodReady(Passive or Inactive) state.To optimize on perormance, instead of traversing the bean all through from create to method ready state, container manages the bean between active and passive states through the container callbacks - ejbActivate() and ejbPassivate() there by managing the bean pool.

    sreenut

    0 讨论(0)
  • 2020-12-02 19:36

    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.

    0 讨论(0)
  • 2020-12-02 19:38

    Methods by nature ARE THREAD SAFE (including static). Why? Simple, because every variable inside the method is created in the stack memory, i.e. every variable used inside the method is created per call (it's not shared). However, parameters aren't part of the stack.

    However, a method is unsafe if it uses an unsafe variable:

    a) calling a static field or variable. However, it happens in every single case.

    b) calling a resource that it's shared. Such as the EntityManager.

    c) passing a parameter that is not safe.

    0 讨论(0)
  • 2020-12-02 19:42

    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.)

    0 讨论(0)
提交回复
热议问题