Java 什么时候使用有状态会话 bean 而不是无状态会话 bean?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18000431/
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
When to use Stateful session bean over Stateless session bean?
提问by sheidaei
A stateful session bean is defined as follows:
有状态会话 bean 定义如下:
Stateful Session Beans The state of an object consists of the values of its instance variables. In a stateful session bean, the instance variables represent the state of a unique client-bean session. Because the client interacts (“talks”) with its bean, this state is often called the conversational state.
有状态会话 Bean 对象的状态由其实例变量的值组成。在有状态会话 bean 中,实例变量表示唯一客户端-bean 会话的状态。因为客户端与其 bean 交互(“对话”),所以这种状态通常称为会话状态。
A stateless session bean is defined as follows:
无状态会话 bean 定义如下:
Stateless Session Beans 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. When the method is finished, the client-specific state should not be retained. Clients may, however, change the state of instance variables in pooled stateless beans, and this state is held over to the next invocation of the pooled stateless bean. Except during method invocation, all instances of a stateless bean are equivalent, allowing the EJB container to assign an instance to any client. That is, the state of a stateless session bean should apply accross all clients.
无状态会话 Bean 无状态会话 Bean 不维护与客户端的对话状态。当客户端调用无状态 bean 的方法时,bean 的实例变量可能包含特定于该客户端的状态,但仅限于调用期间。方法完成后,不应保留客户端特定的状态。但是,客户端可以更改池化无状态 bean 中实例变量的状态,并且该状态保留到池化无状态 bean 的下一次调用中。除了在方法调用期间,无状态 bean 的所有实例都是等效的,允许 EJB 容器将实例分配给任何客户端。也就是说,无状态会话 bean 的状态应该适用于所有客户端。
The advantage of using a stateless session bean over stateful session bean is as follows:
与有状态会话 bean 相比,使用无状态会话 bean 的优点如下:
Because stateless session beans can support multiple clients, they can offer better scalability for applications that require large numbers of clients. Typically, an application requires fewer stateless session beans than stateful session beans to support the same number of clients.
因为无状态会话 bean 可以支持多个客户端,所以它们可以为需要大量客户端的应用程序提供更好的可伸缩性。通常,应用程序需要比有状态会话 bean 更少的无状态会话 bean 来支持相同数量的客户端。
So the question that comes to mind is when one should use stateful session beans? To my naive understanding of the matter, one should stick to use a stateless session bean as he can.
所以我想到的问题是什么时候应该使用有状态会话 bean?就我对此事的天真理解而言,应该尽可能坚持使用无状态会话 bean。
What would be the candidates in which one should use stateful session bean? Any good examples?
哪些人应该使用有状态会话 bean?有什么好的例子吗?
采纳答案by tobiasdenzler
First you have to understand how the beans are created and handled on the server.
首先,您必须了解如何在服务器上创建和处理 bean。
For stateless session beansthe server can maintain a variable amount of instances in a pool. Each time a client requests such a stateless bean (e.g. through a method) a random instance is chosen to serve that request. That means if the client does two subsequent requests it is possible that two different instances of the stateless bean serve the requests. In fact there is no conversational state between the two requests. Also if the client disappears, the stateless bean does not get destroyed and can serve the next request from another client.
对于无状态会话 bean,服务器可以在池中维护可变数量的实例。每次客户端请求这样的无状态 bean(例如通过方法)时,都会选择一个随机实例来为该请求提供服务。这意味着如果客户端执行两个后续请求,则无状态 bean 的两个不同实例可能会为这些请求提供服务。事实上,两个请求之间没有对话状态。此外,如果客户端消失,无状态 bean 不会被破坏,并且可以为来自另一个客户端的下一个请求提供服务。
On the other hand a stateful session beanis closely connected to the client. Each instance is created and bounded to a single client and serves only requests from that particular client. So happens that if you do two subsequent requests on a stateful bean, your request will be served always from the same instance of the bean. That means you can maintain a conversational state between the requests. At the end of the lifecyle, the client calls a remove method and the bean is being destroyed/ready for garbage collection.
另一方面,有状态会话 bean与客户端紧密相连。每个实例都被创建并绑定到单个客户端,并且仅服务于来自该特定客户端的请求。碰巧的是,如果您对一个有状态的 bean 执行两个后续请求,您的请求将始终从 bean 的同一个实例中得到服务。这意味着您可以在请求之间保持对话状态。在生命周期结束时,客户端调用 remove 方法并且 bean 被销毁/准备好进行垃圾收集。
When to use stateless or stateful?
何时使用无状态或有状态?
That mainly depends on whether you want to maintain the conversational state. For example if you have a method that adds up two numbers and return the result you use a stateless bean because its a one time operation. If you call this method a second time with other numbers you are not interested in the result of the previous addition anymore.
这主要取决于您是否要保持会话状态。例如,如果您有一个将两个数字相加并返回结果的方法,您将使用无状态 bean,因为它是一次操作。如果您第二次使用其他数字调用此方法,您将不再对前一次加法的结果感兴趣。
But if you want, for example, count the number of requests a client has done, you have to use a stateful bean. In this scenario it is important to know how often the client has requested the bean method before, so you have to maintain conversational state in the bean (e.g. with a variable). If you would use a stateless bean here, the request of the client would be served each time from a different bean, what messes up your results.
但是,例如,如果您想计算客户端完成的请求数,则必须使用有状态 bean。在这种情况下,了解客户端之前请求 bean 方法的频率很重要,因此您必须在 bean 中维护对话状态(例如,使用变量)。如果您在这里使用无状态 bean,则每次都会从不同的 bean 处理客户端的请求,这会弄乱您的结果。
回答by BSeitkazin
I think that the greatest example of using a Stateful session beanis for a Shopping Cart, where you store all products which user wants to buy.
我认为使用Stateful 会话 bean的最佳示例是用于Shopping Cart,您可以在其中存储用户想要购买的所有产品。