简体   繁体   English

为什么池无状态会话bean?

[英]Why pool Stateless session beans?

Stateless beans in Java do not keep their state between two calls from the client. Java中的无状态bean不会在客户端的两次调用之间保持状态。 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. 当方法返回时,将从堆栈中删除本地,如果分配了一些临时对象,则无论如何都会对它们进行垃圾回收。

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的一个实例而不是汇集其中的一个?

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,你可以保证线程安全(例如,Servlets不是线程安全的)。

Two, you reduce any potential startup time that a bean might have. 二,减少bean可能具有的任何潜在启动时间。 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. 例如,在EJB中,您可以将多个服务器资源注入会话Bean。 That state is private to the bean, but there's no reason you can't keep it from invocation to invocation. 该状态对bean是私有的,但是没有理由不能将它从调用保持到调用。 So, by pooling beans you reduce these lookups to only happening when the bean is created. 因此,通过池化bean,可以将这些查找减少到仅在创建bean时发生。

Three, you can use bean pool as a means to throttle traffic. 三,您可以使用bean池作为节流量的手段。 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. 如果池中只有10个Bean,那么您最多只能获得10个同时工作的请求,其余的将排队等候。

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. 容器可以根据实际活动管理池大小。

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; 存在这种简化,因此不必实现任何特定的接口来直接与UserTransaction对象交互; 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.) 当从InitialContext检索事务(或注入到会话bean)时,它被绑定到线程局部变量以供重用(例如,如果无状态会话bean中的方法调用另一个也使用注入事务的无状态会话bean。 )

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. Statelesss会话bean的生命周期不存在,Passive和MethodReady(被动或非活动)状态。要优化perormance,而不是遍历bean从创建到方法就绪状态,容器通过以下方式管理bean在主动和被动状态之间通过管理bean池,容器回调--ejbActivate()和ejbPassivate()。

sreenut sreenut

Methods by nature ARE THREAD SAFE (including static). 本质上的方法是线索安全(包括静态)。 Why? 为什么? Simple, because every variable inside the method is created in the stack memory, ie 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. a)调用静态字段或变量。 However, it happens in every single case. 但是,它发生在每一个案例中。

b) calling a resource that it's shared. b)调用它共享的资源。 Such as the EntityManager. 比如EntityManager。

c) passing a parameter that is not safe. c)传递不安全的参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM