@Singleton bean由于未预期的事务状态而无法初始化 [英] @Singleton bean failed to initialize because of not expected transaction status

查看:393
本文介绍了@Singleton bean由于未预期的事务状态而无法初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个场景,我需要在应用程序启动时加载一个bean(ReportManager),因此它可以安排执行报告(由DataStore bean从数据库中轮询)。

I had a scenario in which I needed one bean (ReportManager) to be loaded at application startup, so it can schedule reports for execution (polled from database by DataStore bean).

谷歌搜索我发现@Singleton,@ Startup和@DependsOn注释,我用过这样的:

Googling around I found @Singleton, @Startup and @DependsOn annotations, which I've used like this:

@Singleton
@Startup
@DependsOn("DataStore")
public class ReportManager {
    @EJB
    DataStore dataStore;

    @PostConstruct
    public void scheduleReports() {
       logger.log("INITIALIZED");
       List<Report> reports = dataStore.getReports();
       ....
    }
}

@Singleton
@RolesAllowed("user") //I had security checks implemented like that beforehand
public class DataStore {
    @PostConstruct
    public void initialize() {
        logger.log("INITIALIZED");
    }

    public List<Report> getReports() {
        ...
    }
}

问题是我在部署期间遇到了非常奇怪的异常:

The problem was that I was getting really strange exception during deployment time:

<2012-09-27 14:04:15 CEST> <Warning> <Deployer> <BEA-149004> <Failures were detected while initiating deploy task for application "app".>
<2012-09-27 14:04:15 CEST> <Warning> <Deployer> <BEA-149078> <Stack trace for message 149004
weblogic.application.ModuleException: Exception starting module: EJBModule(app-ejb-1.0-SNAPSHOT.jar)

Unable to deploy EJB: ReportManager from app-ejb-1.0-SNAPSHOT.jar:

Singleton ReportManager(Application: app, EJBComponent: app-ejb-1.0-SNAPSHOT.jar) failed to initialize.

   at weblogic.ejb.container.deployer.EJBModule.start(EJBModule.java:592)
 .....

Caused By: weblogic.ejb.container.InternalException: Transaction marked rollback or not expected transaction status: 1
   at weblogic.ejb.container.manager.SingletonSessionManager.postCallback(SingletonSessionManager.java:464)

不是真正的异常异常消息。特别是我也没有得到任何INITIALIZED日志条目。当我注释掉dataStore.getReports()调用时,一切都运行正常,并且bean按正确顺序构造(生成了INITIALIZED消息)。包括dataStore方法调用导致上面的错误,并以某种方式压制所有日志输出。

Not really heplful exception message. Especially that I was also not getting any "INITIALIZED" log entries. When I commented out the dataStore.getReports() invocation everything was working fine and beans were constructed in proper order ("INITIALIZED" messages were produced). Including dataStore method invocation was causing above error and was somehow supressing all the log output.

我正在使用Weblogic 12c。

I am using Weblogic 12c.

推荐答案

最后我弄清楚导致错误的原因。由于空安全上下文而未在 @PostConstruct 方法中设置,因此 @RolesAllowed 声明阻塞了方法调用在 @Startup bean中执行时(来自 EJB 3.1规范,ch.4.3.4 :PostConstruct生命周期回调拦截器方法在未指定的安全上下文中执行。)。

Finally I've figured out what was causing the error. It was the @RolesAllowed declaration which was blocking method invocation due to the empty security context, not set in @PostConstruct method when executed in @Startup bean (From EJB 3.1 spec, ch. 4.3.4: The PostConstruct lifecycle callback interceptor methods execute in an unspecified security context.).

使其工作所需的只是在调用的方法中添加 @PermitAll

What was needed to make it work was just the addition of @PermitAll to the invoked method:

@PermitAll
public List<Report> getReports() {
   ...
}

错误如此误解我决定把这个案子放在这里,因为我无法谷歌答案。

The error was so misleasing I've decided to put this case here, as I couldn't google the answer.

这篇关于@Singleton bean由于未预期的事务状态而无法初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆