EJB 3和Spring @Transactional

时间:2014-03-27 12:54:37

标签: spring hibernate jboss transactions ejb

由于某种原因,我的一个ejb bean间接调用了一个用spring @Transactional注释注释的DAO

当我使用上面的设置运行代码时,我得到一个如下所示的事务异常

    javax.ejb.EJBException: org.springframework.transaction.TransactionSystemException: Could not commit Hibernate transaction; nested exception is org.hibernate.TransactionException: commit failed
at org.jboss.as.ejb3.tx.CMTTxInterceptor.handleExceptionInOurTx(CMTTxInterceptor.java:166) ~[jboss-as-ejb3-7.1.1.Final.jar!/:7.1.1.Final]
at org.jboss.as.ejb3.tx.CMTTxInterceptor.invokeInOurTx(CMTTxInterceptor.java:230) ~[jboss-as-ejb3-7.1.1.Final.jar!/:7.1.1.Final]
at org.jboss.as.ejb3.tx.CMTTxInterceptor.required(CMTTxInterceptor.java:304) ~[jboss-as-ejb3-7.1.1.Final.jar!/:7.1.1.Final]
at org.jboss.as.ejb3.tx.CMTTxInterceptor.processInvocation(CMTTxInterceptor.java:190) ~[jboss-as-ejb3-7.1.1.Final.jar!/:7.1.1.Final]
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) ~[jboss-invocation-1.1.1.Final.jar!/:1.1.1.Final]
at org.jboss.as.ejb3.component.interceptors.CurrentInvocationContextInterceptor.processInvocation(CurrentInvocationContextInterceptor.java:41) ~[jboss-as-ejb3-7.1.1.Final.jar!/:7.1.1.Final]
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) ~[jboss-invocation-1.1.1.Final.jar!/:1.1.1.Final]
at org.jboss.as.ejb3.component.interceptors.LoggingInterceptor.processInvocation(LoggingInterceptor.java:59) ~[jboss-as-ejb3-7.1.1.Final.jar!/:7.1.1.Final]
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) ~[jboss-invocation-1.1.1.Final.jar!/:1.1.1.Final]
at org.jboss.as.ee.component.NamespaceContextInterceptor.processInvocation(NamespaceContextInterceptor.java:50) ~[jboss-as-ee-7.1.1.Final.jar!/:7.1.1.Final]
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) ~[jboss-invocation-1.1.1.Final.jar!/:1.1.1.Final]
at org.jboss.as.ejb3.component.interceptors.AdditionalSetupInterceptor.processInvocation(AdditionalSetupInterceptor.java:43) ~[jboss-as-ejb3-7.1.1.Final.jar!/:7.1.1.Final]
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) ~[jboss-invocation-1.1.1.Final.jar!/:1.1.1.Final]
at org.jboss.as.ee.component.TCCLInterceptor.processInvocation(TCCLInterceptor.java:45) ~[jboss-as-ee-7.1.1.Final.jar!/:7.1.1.Final]
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) ~[jboss-invocation-1.1.1.Final.jar!/:1.1.1.Final]
at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:61) ~[jboss-invocation-1.1.1.Final.jar!/:1.1.1.Final]
at org.jboss.as.ee.component.ViewService$View.invoke(ViewService.java:165) ~[jboss-as-ee-7.1.1.Final.jar!/:7.1.1.Final]
at org.jboss.as.ee.component.ViewDescription$1.processInvocation(ViewDescription.java:173) ~[jboss-as-ee-7.1.1.Final.jar!/:7.1.1.Final]
at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) ~[jboss-invocation-1.1.1.Final.jar!/:1.1.1.Final]
at org.jboss.as.ejb3.component.interceptors.AsyncFutureInterceptorFactory$1$1.runInvocation(AsyncFutureInterceptorFactory.java:71) ~[jboss-as-ejb3-7.1.1.Final.jar!/:7.1.1.Final]
at org.jboss.as.ejb3.component.interceptors.AsyncInvocationTask.run(AsyncInvocationTask.java:73) ~[jboss-as-ejb3-7.1.1.Final.jar!/:7.1.1.Final]
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source) ~[na:1.6.0_29]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) ~[na:1.6.0_29]
at java.lang.Thread.run(Unknown Source) [na:1.6.0_29]
at org.jboss.threads.JBossThread.run(JBossThread.java:122) ~[na:na]

我对事务管理器的bean定义类似于

    <bean id="transactionManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory">
        <ref bean="sessionFactory" />
    </property>
</bean>

任何人都可以了解问题是什么以及我应该如何纠正它?

1 个答案:

答案 0 :(得分:0)

尝试使用@TransactionAttribute(NOT_SUPPORTED)标记EJB方法。

但是,如果你想一起使用Spring和EJB,甚至使用Hibernate(JPA),你应该只依赖于容器服务:允许通过JBOSS引发JPA,从JNDI获取Spring的事务管理器(刚好足以添加) <tx:jta-transaction-manager/>)等等。

另一方面,如果你有Spring,你为什么需要EJB?