无状态 bean 如何处理单例 bean?

How stateless beans deal with singleton ones?

当我有一个使用单例 JPA DAO 的无状态服务 class 并且许多客户端同时请求相同的方法时,EJB 会堆积剩余的请求吗?是否有 time/request 等待列表大小限制?它知道一次只有一个无状态的人应该访问 DAO 吗?如果是,它是如何做到的?

我认为如果发出 100 个请求,将实例化 100 个无状态 bean,但只有一个 DAO 实例。这会引发异常还是会发生某种管理?拥有 DAO 确实会使无状态服务池变得无用?

最后,什么是正确行为所必需的,我的意思是,无状态 bean 排队等候使用单例 DAO?

Singleton 的访问由 EJB 容器同步。查看关于并发管理的 documentation 部分。这来自文档:

Singleton session beans are designed for concurrent access, situations in which many clients need to access a single instance of a session bean at the same time. A singleton’s client needs only a reference to a singleton in order to invoke any business methods exposed by the singleton and doesn’t need to worry about any other clients that may be simultaneously invoking business methods on the same singleton.

我推荐使用这部分文档:

Annotate a singleton’s business or timeout method with @Lock(READ) if the method can be concurrently accessed, or shared, with many clients. Annotate the business or timeout method with @Lock(WRITE) if the singleton session bean should be locked to other clients while a client is calling that method. Typically, the @Lock(WRITE) annotation is used when clients are modifying the state of the singleton.

或者您可以使用bean-managed concurency自行控制同步过程。然后你需要添加 synchronized 锁或任何你用来控制对共享资源的访问。在这种情况下容器将不会参与。

不,它不会抛出任何异常,如果您使用

,容器将使您的 99 个线程等待,而另一个线程将访问该方法
@ConcurrencyManagement(ConcurrencyManagementType.CONTAINER)

@Lock(WRITE)  

几乎与使用同步块一样。

但是,如果您假设在一个同步方法上可能会阻塞这么多线程,这看起来是一个糟糕的设计。

为什么你需要你的 DAO 是单例的?我会说这不是典型的。