我想知道...假设我在 ejb 3.1 中有两个无状态 bean:
@无状态 1级
@EJB MyUniqueInstanceBean uniqueBean1;
2.
@Stateless
Class2
@EJB MyUniqueInstanceBean uniqueBean2;
uniqueBean1 和 uniqueBean2 是否保证是 MyUniqueInstanceBean 的唯一实例?
最佳答案
如果 MyUniqueInstanceBean 是无状态的,那么对 uniquebean1 和 uniquebean2 的调用实际上并不在您手中,而是对同一实例的调用。在 EJB 3.1 规范中,这是通过以下文字来说明的:
Because all instances of a stateless session bean are equivalent, the container can choose to delegate a client-invoked method to any available instance. This means, for example, that the container may delegate the requests from the same client within the same transaction to different instances, and that the container may interleave requests from multiple transactions to the same instance.
如果 MyUniqueInstanceBean 是有状态的,则保证 uniquebean1 和 uniquebean2 不会引用同一实例。再次来自规范:
A session bean instance’s life starts when a client obtains a reference to a stateful session bean instance through dependency injection or JNDI lookup, or when the client invokes a create method on the session bean’s home interface. This causes the container to invoke newInstance on the session bean class to create a new session bean instance.
如果您使用Singleton,那么两者都引用同一个实例,因为只有一个实例:
A Singleton session bean is a session bean component that is instantiated once per application. In cases where the container is distributed over many virtual machines, each application will have one bean instance of the Singleton for each JVM.
关于ejb - @Stateless bean 和 @EJB 保证是唯一的 ejb 实例吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10538945/