用户反馈,在使用 ShardingSphere + Narayana 执行 XA 事务时,发生报错:java.sql.SQLException: javax.transaction.RollbackException: TransactionImple.enlistResource - ARJUNA016064: The transaction is in an invalid state!。 这个报错的含义简单来说就是这个事务已经在其他地方被标记成只可回滚了,不能再进行后续其他的操作。报错信息如下:
Caused by: java.sql.SQLException: javax.transaction.RollbackException: TransactionImple.enlistResource - ARJUNA016064: The transaction is in an invalid state! at org.apache.shardingsphere.transaction.xa.XAShardingSphereTransactionManager.getConnection(XAShardingSphereTransactionManager.java:101) at org.apache.shardingsphere.transaction.ConnectionTransaction.getConnection(ConnectionTransaction.java:102) at org.apache.shardingsphere.driver.jdbc.core.connection.DriverDatabaseConnectionManager.createConnection(DriverDatabaseConnectionManager .java:416) at org.apache.shardingsphere.driver.jdbc.core.connection.DriverDatabaseConnectionManager.createConnections(DriverDatabaseConnectionManage r.java:383) at org.apache.shardingsphere.driver.jdbc.core.connection.DriverDatabaseConnectionManager.getConnections(DriverDatabaseConnectionManager.j ava:357) at org.apache.shardingsphere.driver.jdbc.core.connection.DriverDatabaseConnectionManager.getConnections(DriverDatabaseConnectionManager.j ava:338) at org.apache.shardingsphere.infra.executor.sql.prepare.driver.DriverExecutionPrepareEngine.group(DriverExecutionPrepareEngine.java:89) at org.apache.shardingsphere.infra.executor.sql.prepare.AbstractExecutionPrepareEngine.prepare(AbstractExecutionPrepareEngine.java:73) at org.apache.shardingsphere.infra.executor.sql.prepare.AbstractExecutionPrepareEngine.prepare(AbstractExecutionPrepareEngine.java:61) at org.apache.shardingsphere.driver.jdbc.core.statement.ShardingSpherePreparedStatement.createExecutionGroupContext(ShardingSpherePrepare dStatement.java:764) at org.apache.shardingsphere.driver.jdbc.core.statement.ShardingSpherePreparedStatement.useDriverToExecute(ShardingSpherePreparedStatement.java:717) at org.apache.shardingsphere.driver.jdbc.core.statement.ShardingSpherePreparedStatement.executeWithExecutionContexts(ShardingSpherePreparedStatement.java:658) at org.apache.shardingsphere.driver.jdbc.core.statement.ShardingSpherePreparedStatement.execute(ShardingSpherePreparedStatement.java:631) at org.apache.ibatis.executor.statement.PreparedStatementHandler.update(PreparedStatementHandler.java:44) at org.apache.ibatis.executor.statement.RoutingStatementHandler.update(RoutingStatementHandler.java:69) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
-- set rollback only [2024-01-23 11:10:08,357],[eciqs-core],[com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionImple],[setRollbackOnly],[XNIO-1 task-1],[INFO],[TransactionImple.setRollbackOnly]
原因,比如一个 xa 事务涉及两个 xa resources,在执行第一个 resource enlist 时候,由于 xid 有问题,start xid 在 mysql 上执行失败了,然后 Naryana 自己调用 com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionImple#setRollbackOnly 方法将当前全局事务状态设置为 ActionStatus.ABORT_ONLY 状态。 那么当事务内第二个 xa resource 执行 enlist resource 操作时,由于当前事务已经标记为只回滚,所以执行报错。也就是最开始日志里报的错:java.sql.SQLException: javax.transaction.RollbackException: TransactionImple.enlistResource - ARJUNA016064: The transaction is in an invalid state! 相关代码如下: