Skip to content

Commit c55ff6d

Browse files
authored
bugfix: fix XA rollback on commit failure (#6492) (#6496)
1 parent 5467933 commit c55ff6d

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

changes/en-us/2.x.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@ 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
- [[#6497](https://github.com/apache/incubator-seata/pull/6497)] fix tcc properties class when autoconfigure
4041

42+
4143
### optimize:
4244
- [[#6031](https://github.com/apache/incubator-seata/pull/6031)] add a check for the existence of the undolog table
4345
- [[#6089](https://github.com/apache/incubator-seata/pull/6089)] modify the semantics of RaftServerFactory and remove unnecessary singleton
@@ -187,6 +189,8 @@ Thanks to these contributors for their code commits. Please report an unintended
187189
- [yixia](https://github.com/wt-better)
188190
- [MikhailNavitski](https://github.com/MikhailNavitski)
189191
- [deung](https://github.com/deung)
192+
- [tanyaofei](https://github.com/tanyaofei)
190193
- [xjlgod](https://github.com/xjlgod)
191194

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

changes/zh-cn/2.x.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@
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
- [[#6497](https://github.com/apache/incubator-seata/pull/6497)] 修复自动装配时的seata tcc 配置类
4041

42+
4143
### optimize:
4244
- [[#6031](https://github.com/apache/incubator-seata/pull/6031)] 添加undo_log表的存在性校验
4345
- [[#6089](https://github.com/apache/incubator-seata/pull/6089)] 修改RaftServerFactory语义并删除不必要的单例构建
@@ -185,6 +187,8 @@
185187
- [yixia](https://github.com/wt-better)
186188
- [MikhailNavitski](https://github.com/MikhailNavitski)
187189
- [deung](https://github.com/deung)
190+
- [tanyaofei](https://github.com/tanyaofei)
188191
- [xjlgod](https://github.com/xjlgod)
189192

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

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

Lines changed: 20 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 committing 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,7 +285,7 @@ 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

@@ -307,6 +323,7 @@ protected synchronized void closeForce() throws SQLException {
307323
}
308324
// Force close the physical connection
309325
physicalConn.close();
326+
xaEnded = false;
310327
rollBacked = false;
311328
cleanXABranchContext();
312329
originalConnection.close();

0 commit comments

Comments
 (0)