Skip to content

Commit 9eced11

Browse files
committed
bugfix: fix XA rollback on commit failure (#6492)
1 parent 314b609 commit 9eced11

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

changes/en-us/2.x.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Add changes here for all PR submitted to the 2.x branch.
3636
- [[#6385](https://github.com/apache/incubator-seata/pull/6385)] fix the bug where Role.participant does not execute hooks but clears them.
3737
- [[#6465](https://github.com/apache/incubator-seata/pull/6465)] fix(6257): fix saga mode replay context lost start in 2.x
3838
- [[#6469](https://github.com/apache/incubator-seata/pull/6469)] fix Error in insert sql of [lock_table] data table to sqlserver database
39+
- [[#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)
3940

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

190192
Also, we receive many valuable issues, questions and advices from our community. Thanks for you all.

changes/zh-cn/2.x.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
- [[#6385](https://github.com/apache/incubator-seata/pull/6385)] 修复Role.Participant不执行hook但会清理的问题
3737
- [[#6465](https://github.com/apache/incubator-seata/pull/6465)] 修复2.0下saga模式的context replay丢失start问题
3838
- [[#6469](https://github.com/apache/incubator-seata/pull/6469)] 修复在sqlserver数据库下[lock_table]数据表的插入操作sql中存在的错误
39+
- [[#6492](https://github.com/apache/incubator-seata/pull/6492)] 修复XA执行长时间SQL(或死锁SQL)没有完成回滚就释放连接
3940

4041

4142
### optimize:
@@ -185,5 +186,6 @@
185186
- [yixia](https://github.com/wt-better)
186187
- [MikhailNavitski](https://github.com/MikhailNavitski)
187188
- [deung](https://github.com/deung)
189+
- [tanyaofei](https://github.com/tanyaofei)
188190

189191
同时,我们收到了社区反馈的很多有价值的issue和建议,非常感谢大家。

rm-datasource/src/main/java/org/apache/seata/rm/datasource/xa/ConnectionProxyXA.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ public class ConnectionProxyXA extends AbstractConnectionProxyXA implements Hold
5555

5656
private volatile boolean xaActive = false;
5757

58+
private volatile boolean xaEnded = false;
59+
5860
private volatile boolean kept = false;
5961

6062
private volatile boolean rollBacked = false;
@@ -111,6 +113,13 @@ private void releaseIfNecessary() {
111113
}
112114
}
113115

116+
private void xaEnd(XAXid xaXid, int flags) throws XAException {
117+
if (!xaEnded) {
118+
xaResource.end(xaXid, flags);
119+
xaEnded = true;
120+
}
121+
}
122+
114123
/**
115124
* XA commit
116125
* @param xid global transaction xid
@@ -206,7 +215,14 @@ public synchronized void commit() throws SQLException {
206215
}
207216
try {
208217
// XA End: Success
209-
end(XAResource.TMSUCCESS);
218+
try {
219+
end(XAResource.TMSUCCESS);
220+
} catch (SQLException sqle) {
221+
// Rollback immediately before the XA Branch Context is deleted.
222+
String xaBranchXid = this.xaBranchXid.toString();
223+
rollback();
224+
throw new SQLException("Branch " + xaBranchXid + " was rollbacked on commiting since " + sqle.getMessage(), SQLSTATE_XA_NOT_END, sqle);
225+
}
210226
long now = System.currentTimeMillis();
211227
checkTimeout(now);
212228
setPrepareTime(now);
@@ -234,7 +250,7 @@ public void rollback() throws SQLException {
234250
try {
235251
if (!rollBacked) {
236252
// XA End: Fail
237-
xaResource.end(this.xaBranchXid, XAResource.TMFAIL);
253+
xaEnd(xaBranchXid, XAResource.TMFAIL);
238254
xaRollback(xaBranchXid);
239255
}
240256
// Branch Report to TC
@@ -269,10 +285,11 @@ private synchronized void start() throws XAException, SQLException {
269285
}
270286

271287
private synchronized void end(int flags) throws XAException, SQLException {
272-
xaResource.end(xaBranchXid, flags);
288+
xaEnd(xaBranchXid, flags);
273289
termination();
274290
}
275291

292+
276293
private void cleanXABranchContext() {
277294
branchRegisterTime = null;
278295
prepareTime = null;
@@ -307,6 +324,7 @@ protected synchronized void closeForce() throws SQLException {
307324
}
308325
// Force close the physical connection
309326
physicalConn.close();
327+
xaEnded = false;
310328
rollBacked = false;
311329
cleanXABranchContext();
312330
originalConnection.close();

0 commit comments

Comments
 (0)