我正在学习 EJB,到目前为止我已经读到 EJB 中不允许使用多线程,因为容器应该关心线程安全并让开发人员只关注业务逻辑,所以基本上意味着 EJB 确保只有一个线程可以同时访问 Session bean 中的方法。
当我们有许多用户访问 EJB 中的同一个方法时会发生什么? 容器是序列化访问,还是创建不同的 bean 实例,每个线程一个?
谁能解释一下这方面的政策是什么?另外我有点困惑,为什么如果不允许多线程,那么我们不能创建自己的线程,为什么我们有这个 @Asynchronous 注释?
最佳答案
是的,它创建了多个实例,并将它们集中在一起。查看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.
关于java - EJB 中的多线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30013472/