java - 单例 EJB 中的系统异常

标签 java jakarta-ee singleton ejb

我正在阅读 EJB 规范,我对我的观点的矛盾感到困惑,特别是关于 ContainerSystem Exception 时的行为方式Singleton Bean的生​​命周期回调方法中被抛出。

第 12.3.1 节:

A runtime exception thrown by any lifecycle interceptor callback method causes the bean instance and its interceptors to be discarded after the interceptor chain unwindsce and any associated interceptor instances are discarded (except for Singleton Beans).

第 4.8.4 节:

Errors occurring during Singleton initialization are considered fatal and must result in the discarding of the Singleton instance. Possible initialization errors include injection failure, a system exception thrown from a PostConstruct method, or the failure of a PostConstruct method container-managed transaction to successfully commit.

Unlike instances of other component types, system exceptions thrown from business methods or callbacks of a Singleton do not result in the destruction of the Singleton instance.

那么,根据第 4.8.4 节,什么是正确的?如果在 @PostConstruct 方法或 Interceptor @PostConstruct 方法中抛出 System Exception Bean 被丢弃?我试过它抛出 EJBException,结果是 Beans 初始化失败。

更令人惊讶的是,当从 Singleton Bean Business Method 中抛出 EJBException 时,客户端收到了 Exception 并且没有进一步执行其他方法。我可以假设 Bean 实例被丢弃了吗?根据规范,不应导致 Bean 实例 的破坏。在这两种情况下,什么是正确的?

最佳答案

我要求提供代码,因为这可能是供应商错误。正如您阅读的那样,如果在 @PostContruct 时出现 EjbException,则不会构建单例。在那之后,单例方法可以抛出任何类型的异常(已检查或未检查)并且实例应该仍在运行。

拦截器生命周期附加到单例生命周期,因此,如果拦截器无法执行其@PostContruct,则不会创建单例。

要记住的另一件事是:如果 EJB(@Stateless 或 @Stateful)抛出 EjbException(或其拦截器),该实例会自动被丢弃,因此,规范中有一条注释说: [58] 单例除外。参见第 4.8.4 节

关于java - 单例 EJB 中的系统异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21802575/

相关文章:

java - 无法在 jboss 7 上使用 @EJB 注入(inject) EJB 3

java - Spring MVC 区域设置更改不起作用

java - WebLogic 单例服务可以用于等待/通知吗?

c++ - 从 linux 移植到 windows 时单例模板类中的语法错误

c# - Singleton 设计模式是否内置于任何框架中?

java - 如何在 Java 中读取字符直到某一特定字符?

java - 在Java中添加数组

实现oauth2.0的Java库

java - 如何使用单例设计模式java连接到SQL数据库?

java - 如何在 Jcob 中使用 OCX 文件