Skip to content

Commit

Permalink
bugfix: fix XA rollback on commit failure (#6492)
Browse files Browse the repository at this point in the history
  • Loading branch information
tanyaofei committed Apr 23, 2024
1 parent 314b609 commit 9eced11
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
2 changes: 2 additions & 0 deletions changes/en-us/2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Add changes here for all PR submitted to the 2.x branch.
- [[#6385](https://github.com/apache/incubator-seata/pull/6385)] fix the bug where Role.participant does not execute hooks but clears them.
- [[#6465](https://github.com/apache/incubator-seata/pull/6465)] fix(6257): fix saga mode replay context lost start in 2.x
- [[#6469](https://github.com/apache/incubator-seata/pull/6469)] fix Error in insert sql of [lock_table] data table to sqlserver database
- [[#6492](https://github.com/apache/incubator-seata/pull/6492)] fix XA did not rollback but close when executing a long-running SQL(or deadlock SQL)

### optimize:
- [[#6031](https://github.com/apache/incubator-seata/pull/6031)] add a check for the existence of the undolog table
Expand Down Expand Up @@ -186,5 +187,6 @@ Thanks to these contributors for their code commits. Please report an unintended
- [yixia](https://github.com/wt-better)
- [MikhailNavitski](https://github.com/MikhailNavitski)
- [deung](https://github.com/deung)
- [tanyaofei](https://github.com/tanyaofei)

Also, we receive many valuable issues, questions and advices from our community. Thanks for you all.
2 changes: 2 additions & 0 deletions changes/zh-cn/2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
- [[#6385](https://github.com/apache/incubator-seata/pull/6385)] 修复Role.Participant不执行hook但会清理的问题
- [[#6465](https://github.com/apache/incubator-seata/pull/6465)] 修复2.0下saga模式的context replay丢失start问题
- [[#6469](https://github.com/apache/incubator-seata/pull/6469)] 修复在sqlserver数据库下[lock_table]数据表的插入操作sql中存在的错误
- [[#6492](https://github.com/apache/incubator-seata/pull/6492)] 修复XA执行长时间SQL(或死锁SQL)没有完成回滚就释放连接


### optimize:
Expand Down Expand Up @@ -185,5 +186,6 @@
- [yixia](https://github.com/wt-better)
- [MikhailNavitski](https://github.com/MikhailNavitski)
- [deung](https://github.com/deung)
- [tanyaofei](https://github.com/tanyaofei)

同时,我们收到了社区反馈的很多有价值的issue和建议,非常感谢大家。
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ public class ConnectionProxyXA extends AbstractConnectionProxyXA implements Hold

private volatile boolean xaActive = false;

private volatile boolean xaEnded = false;

private volatile boolean kept = false;

private volatile boolean rollBacked = false;
Expand Down Expand Up @@ -111,6 +113,13 @@ private void releaseIfNecessary() {
}
}

private void xaEnd(XAXid xaXid, int flags) throws XAException {
if (!xaEnded) {
xaResource.end(xaXid, flags);
xaEnded = true;
}
}

/**
* XA commit
* @param xid global transaction xid
Expand Down Expand Up @@ -206,7 +215,14 @@ public synchronized void commit() throws SQLException {
}
try {
// XA End: Success
end(XAResource.TMSUCCESS);
try {
end(XAResource.TMSUCCESS);
} catch (SQLException sqle) {
// Rollback immediately before the XA Branch Context is deleted.
String xaBranchXid = this.xaBranchXid.toString();
rollback();
throw new SQLException("Branch " + xaBranchXid + " was rollbacked on commiting since " + sqle.getMessage(), SQLSTATE_XA_NOT_END, sqle);
}
long now = System.currentTimeMillis();
checkTimeout(now);
setPrepareTime(now);
Expand Down Expand Up @@ -234,7 +250,7 @@ public void rollback() throws SQLException {
try {
if (!rollBacked) {
// XA End: Fail
xaResource.end(this.xaBranchXid, XAResource.TMFAIL);
xaEnd(xaBranchXid, XAResource.TMFAIL);
xaRollback(xaBranchXid);
}
// Branch Report to TC
Expand Down Expand Up @@ -269,10 +285,11 @@ private synchronized void start() throws XAException, SQLException {
}

private synchronized void end(int flags) throws XAException, SQLException {
xaResource.end(xaBranchXid, flags);
xaEnd(xaBranchXid, flags);
termination();
}


private void cleanXABranchContext() {
branchRegisterTime = null;
prepareTime = null;
Expand Down Expand Up @@ -307,6 +324,7 @@ protected synchronized void closeForce() throws SQLException {
}
// Force close the physical connection
physicalConn.close();
xaEnded = false;
rollBacked = false;
cleanXABranchContext();
originalConnection.close();
Expand Down

0 comments on commit 9eced11

Please sign in to comment.