EJB代码中未捕获事务提交错误

时间:2018-01-11 18:50:23

标签: database jpa java-ee transactions wildfly

当我在flush()之后和commit()之前关闭数据库时,会记录一个异常,但代码不会捕获该异常:

@Stateless
@TransactionAttribute(TransactionAttributeType.NEVER)
public class OuterService {

    @EJB InnerService innerService;

    public String outerMethod() {
        try {
            innerService.innerMethod();
            return "success";
        } catch (Exception e) {
            return "failure";
        }
    }
}

@Stateless
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class InnerService {

    @PersistenceContext EntityManager em;

    public void innerMethod() {
        em.persist(new Entity());
        em.flush();
    } //put the breakpoint here
}

我在调试模式下运行代码并在刷新之后但在退出事务方法之前设置断点。执行暂停时,我停止数据库服务,然后恢复代码。

使用以下根本原因记录异常:

  

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException:   commit()期间通信链路失败。交易决议   未知的。

但是外部try..catch块没有捕获它,并且该方法成功完成。似乎JTA实现窒息了异常。如何通知错误?

我已经尝试过BMT和CDI活动,但都没有。但是,Java SE环境中的普通JDBC和JPA(Hibernate,带有内置和C3p0池)确实有效。

我的设置:Ubuntu 17.10,Wildfly 10,MySQL 5.7.20,Connector / J 5.1.44

这是日志(由于字符限制而删除了一些行):

2018-01-07 12:38:44,980 INFO  [stdout] (default task-1) Hibernate: insert into Entity values ( )
2018-01-07 12:39:06,027 WARN  [org.jboss.jca.core.connectionmanager.listener.TxConnectionListener] (default task-1) IJ000305: Connection error occured: org.jboss.jca.core.connectionmanager.listener.TxConnectionListener@f0b0aed[state=NORMAL managed connection=org.jboss.jca.adapters.jdbc.local.LocalManagedConnection@327b7fd0 connection handles=0 lastReturned=1515316110106 lastValidated=1515316098805 lastCheckedOut=1515316124981 trackByTx=true pool=org.jboss.jca.core.connectionmanager.pool.strategy.OnePool@a4e7bad mcp=SemaphoreConcurrentLinkedQueueManagedConnectionPool@42037075[pool=TestDS] xaResource=LocalXAResourceImpl@306327f6[connectionListener=f0b0aed connectionManager=6110d60 warned=false currentXid=null productName=MySQL productVersion=5.7.20-0ubuntu0.17.10.1 jndiName=java:/datasources/TestDS] txSync=TransactionSynchronization@360457732{tx=TransactionImple < ac, BasicAction: 0:ffff7f000101:-7fd727eb:5a51e37d:1d status: ActionStatus.COMMITTING > wasTrackByTx=true enlisted=true cancel=false}]: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Communications link failure during commit(). Transaction resolution unknown.
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
    at com.mysql.jdbc.Util.getInstance(Util.java:408)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:860)
    at com.mysql.jdbc.ConnectionImpl.commit(ConnectionImpl.java:1552)
    at org.jboss.jca.adapters.jdbc.local.LocalManagedConnection.commit(LocalManagedConnection.java:96)
    at org.jboss.jca.core.tx.jbossts.LocalXAResourceImpl.commit(LocalXAResourceImpl.java:172)
    at com.arjuna.ats.arjuna.coordinator.TwoPhaseCoordinator.end(TwoPhaseCoordinator.java:96)
    at com.arjuna.ats.arjuna.AtomicAction.commit(AtomicAction.java:162)
    at com.arjuna.ats.internal.jta.transaction.arjunacore.BaseTransaction.commit(BaseTransaction.java:126)
    at com.arjuna.ats.jbossatx.BaseTransactionManagerDelegate.commit(BaseTransactionManagerDelegate.java:89)
    at org.jboss.as.ejb3.tx.CMTTxInterceptor.processInvocation(CMTTxInterceptor.java:239)
    at org.wildfly.security.manager.WildFlySecurityManager.doChecked(WildFlySecurityManager.java:636)
    at InnerService$$$view26.innerMethod(Unknown Source)
    at OuterService.outerMethod(OuterService.java:23)
    at org.jboss.as.ee.component.ManagedReferenceMethodInterceptor.processInvocation(ManagedReferenceMethodInterceptor.java:52)
    at OuterService$$$view33.outerMethod(Unknown Source)
    at RestManager.test(RestManager.java:112)

2018-01-07 12:39:06,032 WARN  [com.arjuna.ats.jta] (default task-1) ARJUNA016039: onePhaseCommit on < formatId=131077, gtrid_length=47, bqual_length=36, tx_uid=0:ffff7f000101:-7fd727eb:5a51e37d:1d, node_name=mypc, branch_uid=0:ffff7f000101:-7fd727eb:5a51e37d:20, subordinatenodename=null, eis_name=java:/datasources/TestDS > (LocalXAResourceImpl@306327f6[connectionListener=f0b0aed connectionManager=6110d60 warned=false currentXid=null productName=MySQL productVersion=5.7.20-0ubuntu0.17.10.1 jndiName=java:/datasources/TestDS]) failed with exception XAException.XAER_RMFAIL: org.jboss.jca.core.spi.transaction.local.LocalXAException: IJ001156: Could not commit local transaction
    at org.jboss.jca.core.tx.jbossts.LocalXAResourceImpl.commit(LocalXAResourceImpl.java:177)
    at com.arjuna.ats.internal.jta.resources.arjunacore.XAOnePhaseResource.commit(XAOnePhaseResource.java:120)
    at org.jboss.as.ejb3.tx.CMTTxInterceptor.endTransaction(CMTTxInterceptor.java:91)
    at InnerService$$$view26.innerMethod(Unknown Source)
    at OuterService.outerMethod(OuterService.java:23)
    at OuterService$$$view33.outerMethod(Unknown Source)
    at RestManager.test(RestManager.java:112)
    at RestManager$Proxy$_$$_Weld$EnterpriseProxy$.test(Unknown Source)
    at java.lang.Thread.run(Thread.java:748)
Caused by: org.jboss.jca.core.spi.transaction.local.LocalResourceException: Communications link failure during commit(). Transaction resolution unknown.
    at org.jboss.jca.adapters.jdbc.local.LocalManagedConnection.commit(LocalManagedConnection.java:103)
    at org.jboss.jca.core.tx.jbossts.LocalXAResourceImpl.commit(LocalXAResourceImpl.java:172)
    ... 248 more
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Communications link failure during commit(). Transaction resolution unknown.
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:860)
    at com.mysql.jdbc.ConnectionImpl.commit(ConnectionImpl.java:1552)
    at org.jboss.jca.adapters.jdbc.local.LocalManagedConnection.commit(LocalManagedConnection.java:96)
    ... 249 more

2 个答案:

答案 0 :(得分:1)

我对此进行了调查,这对我来说是一个问题。我在这里创建了jira:https://issues.jboss.org/browse/JBTM-2983。如果确认的话,请随意在那里进行讨论。

我希望通知调用者在提交期间有异常。顺便说一句。如果我进行了一些进一步的调查,我会根据您的问题创建一个小型测试项目:https://github.com/ochaloup/catch-ejb-exception-test.git

答案 1 :(得分:0)

我不确定。可能有几个原因。

首先,我要尝试增加mysql配置中的net_write_timeout属性。

此外,您的异常是由错误代码XAException.XAER_RMFAIL

引起的

Jboss Javadoc: XAException

  

错误代码,指示资源管理器已失败但未成功   可用。

因为断点中断,似乎PersistenceContext被破坏或无效。