有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

EJB中的java多线程

我正在学习EJB,到目前为止我已经读到EJB中不允许多线程,因为它是容器,应该关心线程安全,让开发人员只关注业务逻辑,所以基本上这意味着EJB确保只有一个线程可以同时访问会话bean中的方法

当我们有许多用户访问EJB中的同一方法时,会发生什么? 容器是序列化访问,还是创建不同的bean实例,每个线程一个

有人能给我解释一下这方面的政策吗?我也有点困惑,为什么不允许多线程,所以我们不能创建自己的线程,为什么我们有这个@Asynchronous注释


共 (1) 个答案

  1. # 1 楼答案

    是的,它创建了几个实例,并将它们集中在一起。见official Oracle documentation

    Because a stateless session bean is never passivated, its lifecycle has only two stages: nonexistent and ready for the invocation of business methods. Figure 22-4 illustrates the stages of a stateless session bean.

    The EJB container typically creates and maintains a pool of stateless session beans, beginning the stateless session bean’s lifecycle. The container performs any dependency injection and then invokes the method annotated @PostConstruct, if it exists. The bean is now ready to have its business methods invoked by a client.

    At the end of the lifecycle, the EJB container calls the method annotated @PreDestroy, if it exists. The bean’s instance is then ready for garbage collection.