【问题标题】:JavaEE CDI Testing: get cause of RollbackExceptionJavaEE CDI 测试:获取 RollbackException 的原因
【发布时间】:2013-08-06 15:14:38
【问题描述】:

我正在使用带有嵌入式 Glassfish 4.0 容器的 Arquillian 进行测试。到目前为止,我设法让它工作,但是一个测试用例一直失败,我不知道为什么。这是我的测试代码:

// Test Class
    @Inject
    private CompetenceService competenceService;

    @Test
    public void createSingleCompetence() {
        Competence c = new Competence();
        // fill the competence with data
        c = competenceService.save(c); // throws a RollbackException
        // some testing assertions
    }

这是服务类:

// CompetenceService
@Transactional
@ApplicationScoped
public class CompetenceService {

    @PersistenceContext
    private EntityManager em;

    public Competence save(Competence c){
        return em.merge(c);
    }
}

使用 arquillian 运行时,出现以下错误:

 Managed bean with Transactional annotation and TxType of REQUIRED encountered exception during commit javax.transaction.RollbackException: Transaction marked for rollback

我的问题是:我能以某种方式找出问题所在吗?我的第一个想法是并非所有约束都得到满足(例如@NotNull),但我现在排除了这一点。

我没有得到完整的堆栈跟踪,只有测试运行结束时的错误。这是输出:

[Competence test competence]
Aug 07, 2013 8:28:57 AM org.glassfish.cdi.transaction.TransactionalInterceptorRequired transactional
INFO: In REQUIRED TransactionalInterceptor
Aug 07, 2013 8:28:57 AM org.glassfish.cdi.transaction.TransactionalInterceptorRequired transactional
INFO: Managed bean with Transactional annotation and TxType of REQUIRED called outside a transaction context.  Beginning a transaction...
Aug 07, 2013 8:28:57 AM org.glassfish.cdi.transaction.TransactionalInterceptorBase markRollbackIfActiveTransaction
INFO: About to setRollbackOnly from @Transactional interceptor on transaction:JavaEETransactionImpl: txId=1 nonXAResource=1 jtsTx=null localTxStatus=0 syncs=[org.eclipse.persistence.internal.jpa.transaction.JTATransactionWrapper$1@fe04c00, org.eclipse.persistence.transaction.JTASynchronizationListener@4e9d9c24, org.eclipse.persistence.transaction.JTASynchronizationListener@4d7627ce, com.sun.enterprise.resource.pool.PoolManagerImpl$SynchronizationListener@82f6d1d]
Aug 07, 2013 8:28:57 AM org.glassfish.cdi.transaction.TransactionalInterceptorRequired transactional
INFO: Managed bean with Transactional annotation and TxType of REQUIRED encountered exception during commit javax.transaction.RollbackException: Transaction marked for rollback.
Aug 07, 2013 8:28:58 AM org.glassfish.persistence.common.Java2DBProcessorHelper executeDDLs
WARNING: PER01000: Got SQLException executing statement "ALTER TABLE COMPETENCECATEGORY DROP CONSTRAINT CMPTWNNGNSTNCNTTYD": java.sql.SQLSyntaxErrorException: ALTER TABLE failed. There is no constraint 'APP.CMPTWNNGNSTNCNTTYD' on table '"APP"."COMPETENCECATEGORY"'. 
PlainTextActionReporterSUCCESS
PER01003: Deployment encountered SQL Exceptions:
PER01000: Got SQLException executing statement "ALTER TABLE COMPETENCECATEGORY DROP CONSTRAINT CMPTWNNGNSTNCNTTYD": java.sql.SQLSyntaxErrorException: ALTER TABLE failed. There is no constraint 'APP.CMPTWNNGNSTNCNTTYD' on table '"APP"."COMPETENCECATEGORY"'. Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 23.032 sec <<< FAILURE!
Aug 07, 2013 8:29:01 AM org.glassfish.admin.mbeanserver.JMXStartupService shutdown
INFO: JMXStartupService and JMXConnectors have been shut down.
JdbcRuntimeExtension,  getAllSystemRAResourcesAndPools = [GlassFishConfigBean.org.glassfish.jdbc.config.JdbcResource, GlassFishConfigBean.org.glassfish.jdbc.config.JdbcResource, GlassFishConfigBean.org.glassfish.jdbc.config.JdbcConnectionPool, GlassFishConfigBean.org.glassfish.jdbc.config.JdbcConnectionPool, GlassFishConfigBean.org.glassfish.jdbc.config.JdbcConnectionPool, GlassFishConfigBean.org.glassfish.jdbc.config.JdbcResource]
Aug 07, 2013 8:29:01 AM com.sun.enterprise.connectors.service.ResourceAdapterAdminServiceImpl sendStopToResourceAdapter
INFO: RAR7094: __ds_jdbc_ra shutdown successful.
Aug 07, 2013 8:29:01 AM com.sun.enterprise.v3.server.AppServerStartup stop
INFO: Shutdown procedure finished

Results :

Tests in error: 
  createSingleCompetence(at.seresunit.outtasking.test.CompetenceTest): Managed bean with Transactional annotation and TxType of REQUIRED encountered exception during commit javax.transaction.RollbackException: Transaction marked for rollback.

Tests run: 1, Failures: 0, Errors: 1, Skipped: 0

------------------------------------------------------------------------
BUILD FAILURE
------------------------------------------------------------------------

【问题讨论】:

  • 您没有在控制台或返回中获得任何堆栈跟踪或任何内容?我也猜你的异常是在保存方法之后发生的?
  • 嗨,我更新了我得到的输出的问题。另外,由于 save 方法发生了异常,之后没有执行任何语句。

标签: jakarta-ee cdi transactional jboss-arquillian


【解决方案1】:

这不是 Arquillian 的问题。

由于您在 StackTrace 中找不到异常,我想原始异常被包装到 RollbackException 中。根据 JTA 1.2 规范,任何未检查的 Exception 都会自动回滚事务。

您能否确认在您的任何底层方法中都没有抛出未经检查的异常(例如 RuntimeException、Nullpointer 等) - 甚至可能来自某些自定义验证器。 这是供进一步参考的 JTA 1.2 规范:https://java.net/projects/jta-spec/sources/spec-source-repository/content/jta-1_2-spec_v2.pdf?rev=14

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-13
    • 1970-01-01
    • 1970-01-01
    • 2013-10-08
    • 2010-09-11
    相关资源
    最近更新 更多