java 为什么要池化无状态会话 bean?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/134791/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why pool Stateless session beans?
提问by Andrew Андрей Листочкин
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 takes parameters and return results. When the method is invoked some local variables are being created in execution stack. When the method returns the locals are removed from the stack and if some temporary objects were allocated they are garbage collected anyway.
Java 中的无状态 bean 不会在来自客户端的两次调用之间保持它们的状态。因此,简而言之,我们可以将它们视为具有业务方法的对象。每个方法都接受参数并返回结果。调用该方法时,会在执行堆栈中创建一些局部变量。当方法返回时,局部变量从堆栈中移除,如果分配了一些临时对象,它们无论如何都会被垃圾收集。
From my perspective that doesn't differ from calling method of the same single instance by separate threads. So why cannot a container use one instance of a bean instead of pooling a number of them?
从我的角度来看,这与通过单独的线程调用同一单个实例的方法没有区别。那么为什么一个容器不能使用一个 bean 的一个实例,而不是将多个实例集中起来呢?
回答by Will Hartung
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).
一,通过每个实例有一个 bean,您可以保证线程安全(例如,Servlet 不是线程安全的)。
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.
第二,您减少了 bean 可能具有的任何潜在启动时间。虽然会话 Bean 是“无状态的”,但它们只需要在客户端方面是无状态的。例如,在 EJB 中,您可以将多个服务器资源注入到一个会话 Bean 中。该状态对 bean 来说是私有的,但是您没有理由不能在调用之间保持它。因此,通过池化 bean,您可以将这些查找减少到仅在创建 bean 时发生。
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.
三、可以使用bean pool作为节流的手段。如果池中只有 10 个 Bean,则最多只能同时处理 10 个请求,其余请求将排队。
回答by Mwanji Ezana
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.
由于您不知道将使用哪个实例(并且多个线程可以同时使用单个实例),因此 bean 必须是线程安全的。
The container can manage pool size based on actual activity.
容器可以根据实际活动管理池大小。
回答by Kris Nuttycombe
The transactionality of the Java EE model uses the thread context to manage the transaction lifecycle.
Java EE 模型的事务性使用线程上下文来管理事务生命周期。
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.)
存在这种简化,因此无需实现任何特定接口即可直接与 UserTransaction 对象交互;当从 InitialContext 检索事务(或注入会话 bean)时,它会绑定到线程局部变量以供重用(例如,如果无状态会话 bean 中的方法调用另一个也使用注入事务的无状态会话 bean。 )
回答by magallanes
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.
a) 调用静态字段或变量。但是,它发生在每一种情况下。
b) calling a resource that it's shared. Such as the EntityManager.
b) 调用共享的资源。比如EntityManager。
c) passing a parameter that is not safe.
c) 传递不安全的参数。
回答by magallanes
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.
无状态会话 bean 的生命周期是不存在、被动和方法就绪(被动或非活动)状态。为了优化性能,而不是从创建状态到方法就绪状态遍历 bean,容器通过以下方式管理主动和被动状态之间的 bean容器回调 - ejbActivate() 和 ejbPassivate() 通过管理 bean 池。
sreenut
果冻

