ejb - @Stateless bean 和 @EJB 保证是唯一的 ejb 实例吗?

标签 ejb unique ejb-3.1 stateless

我想知道...假设我在 ejb 3.1 中有两个无状态 bean:

  1. @无状态 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/

相关文章:

javascript - 汇总对象数组并计算每个唯一对象名称的平均值

java - 表示层中的实体类?

java - 使用 context.lookup 加载远程接口(interface)时出现问题

eclipse - 使用gradle为EJB模块生成eclipse项目

java - Stateless ejb 不删除池

java - JTA 和 MySQL - 如何从数据库检索记录

MYSQL 如何对两列任意组合(双向)创建UNIQUE约束

arrays - 在 Go slice 或数组中查找唯一项

java - 使用 OpenEJB 控制无状态 session Bean 的名称

jakarta-ee - EJB 无状态初始化模式