为什么是无状态会话bean? [英] Why Stateless session beans?

查看:168
本文介绍了为什么是无状态会话bean?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读有关无状态会话bean,无法理解它的用法。



从下面的sun教程摘录



由于无状态会话bean可以支持多个客户端,因此可以为需要大量客户端的应用程序提供更好的可扩展性



无状态会话bean正在用过的?什么样的应用程序使用它?



在无状态会话bean出现之前,在类似上下文中支持多个客户端的机制是什么?



任何人都可以提供一些细节?



谢谢!

解决方案

说实话,很难找到SLSB的合理用例。由于它们不具有任何状态(如同名称),因此它们本质上是线程安全的。即使它们被集装箱汇集。



另一方面,将它们用作安全的临时存储是有诱惑力的,因为它们保证线程安全(由于池),您不需要任何同步或线程安全的集合。 考虑以下pseude代码:

  @Stateless 
public class Slsb {
private int counter;

public void increment(){
++ counter;
}

public int getCounter(){
return counter;
}
}

客户端:

  @Resource 
private Slsb slsb;

public void clientMethod(){
slsb.increment();
slsb.increment();
slsb.getCounter(); // ???
}

此代码(尽管它的粗俗度)完全正常,不需要 AtomicInteger 例如。



你期望什么结果?实际上,任何非负值都是可能的...任何调用 slsb 可能由 Slsb 的不同实例提供在此期间,您的(以前使用的)实例可能已被用于服务不同的客户端。结论:存储状态在SLSB中是错误的,但是由于某种原因,SLSB被合并以避免在更改状态时出现线程问题(?!?)。 个人我更喜欢单身人士服务(Spring-like),我从来没有SLSB的想法。是的,我知道EJB 3.1中的单例EJB。


I was reading about stateless session bean and couldn't understand it's use.

Excerpt from sun tutorial below

"..Because stateless session beans can support multiple clients, they can offer better scalability for applications that require large numbers of clients"

Where stateless session bean is being used? what kind of applications use it?

What mechanism has been being used before the advent of 'stateless session bean' to support multiple clients in the similar contexts?

Can anyone please provide some details?

thank you!

解决方案

To be honest, it is hard to find any reasonable use case for SLSBs. Since they don't hold any state (as the name imposes), they should be inherently thread-safe. Even though they are pooled by the container.

On the other hand it is tempting to use them as a safe temporary storage as they are guaranteed to be thread-safe (thanks to pooling), you don't need any synchronization or thread-safe collections. But consider the following pseude-code:

@Stateless
public class Slsb {
  private int counter;

  public void increment() {
    ++counter;
  }

  public int getCounter() {
    return counter;
  }
}

Client-side:

@Resource
private Slsb slsb;

public void clientMethod() {
  slsb.increment();
  slsb.increment();
  slsb.getCounter();  //???
}

This code (despite its vulgarity) is perfectly fine and it does not require AtomicInteger for instance.

What result do you expect? Actually, any non-negative value is possible... Any call to slsb might be served by different instance of Slsb and in the meantime your (previously used) instance might have been used to serve different clients. Conclusion: storing state in SLSB is wrong, but for some reason SLSBs are pooled to avoid threading issues when changing the state (?!?). Personally I much prefer singleton services (Spring-like) and I have never got the SLSB idea. And yes, I am aware of singleton EJBs in EJB 3.1.

这篇关于为什么是无状态会话bean?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆