@Singleton bean failed to initialize because of not expected transaction status

前提是你 提交于 2019-12-10 19:31:00

问题


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).

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)

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.

I am using Weblogic 12c.


回答1:


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.).

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.




回答2:


In my case, I'm using SpringLoader.java class which is a @Singleton that loads Spring configuration. Spring is configured with Quartz scheduler that is trying accessing its DB table(s) (as part of some transaction).

So in my case, it helped adding the following line to this SpringLoader class: @TransactionManagement(TransactionManagementType.BEAN)

After that, some more meaningful error message is displayed: ORA-00942: table or view does not exist.

BTW: Its was also worth adding this line: @ConcurrencyManagement(ConcurrencyManagementType.BEAN)



来源:https://stackoverflow.com/questions/12621559/singleton-bean-failed-to-initialize-because-of-not-expected-transaction-status

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!