fix: implement XA mode commitOnXA() and LockQuery()#1061
fix: implement XA mode commitOnXA() and LockQuery()#1061thunguo merged 16 commits intoapache:masterfrom
Conversation
- tx_xa.go: implement commitOnXA() to execute XA END + XA PREPARE and report status to TC - xa_resource_manager.go: LockQuery() delegates to rmRemoting for global lock checking - tx.go: add xaConn field to support XA transactions - conn_xa.go: set xaConn reference in BeginTx
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #1061 +/- ##
==========================================
+ Coverage 57.34% 57.52% +0.17%
==========================================
Files 267 267
Lines 17597 17656 +59
==========================================
+ Hits 10091 10156 +65
+ Misses 6681 6672 -9
- Partials 825 828 +3 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
Do we need to add some test cases? It seems the unit test coverage has decreased somewhat. |
This commit addresses PR apache#1061 feedback and Oracle architectural review findings: **Reviewer Feedback (thunguo):** - Extract anonymous xaConn interface into named XAConnection type for better readability **Code Quality Improvements:** - Add comprehensive logging to commitOnXA() following existing patterns (log.Infof/Errorf) - Add detailed documentation comments explaining XA two-phase commit protocol flow - Improve test coverage with 11 new unit tests for XATx functionality **Critical Bug Fixes:** 1. Fix BranchReport error handling - errors are now properly checked and logged 2. Complete XA Rollback lifecycle - now executes XA END(TMFAIL) + XA ROLLBACK + TC reporting 3. Fix SQL error reporting - createNewTxOnExecIfNeed() now reports PhaseoneFailed to TC 4. Fix nil pointer safety - set tx.target after real transaction is created in XAConn.BeginTx() 5. Update prepareTime after successful XA PREPARE for correct connection hold/timeout behavior **Files Changed:** - pkg/datasource/sql/tx.go: Extract XAConnection interface, add xaConn field - pkg/datasource/sql/tx_xa.go: Implement commitOnXA() with logging and error handling, complete Rollback() lifecycle - pkg/datasource/sql/conn_xa.go: Fix nil pointer issue, add SQL error reporting, update prepareTime - pkg/datasource/sql/tx_xa_test.go: Add comprehensive unit tests (11 tests covering all scenarios) **Test Results:** - All existing tests pass - 11 new tests added and passing - Build successful with no errors Co-authored-by: Oracle Review <architectural-review>
158ed24 to
e010686
Compare
This commit addresses PR apache#1061 feedback and Oracle architectural review findings: **Reviewer Feedback (thunguo):** - Extract anonymous xaConn interface into named XAConnection type for better readability **Code Quality Improvements:** - Add comprehensive logging to commitOnXA() following existing patterns (log.Infof/Errorf) - Add detailed documentation comments explaining XA two-phase commit protocol flow - Improve test coverage with 11 new unit tests for XATx functionality **Critical Bug Fixes:** 1. Fix BranchReport error handling - errors are now properly checked and logged 2. Complete XA Rollback lifecycle - now executes XA END(TMFAIL) + XA ROLLBACK + TC reporting 3. Fix SQL error reporting - createNewTxOnExecIfNeed() now reports PhaseoneFailed to TC 4. Fix nil pointer safety - set tx.target after real transaction is created in XAConn.BeginTx() 5. Update prepareTime after successful XA PREPARE for correct connection hold/timeout behavior **Files Changed:** - pkg/datasource/sql/tx.go: Extract XAConnection interface, add xaConn field - pkg/datasource/sql/tx_xa.go: Implement commitOnXA() with logging and error handling, complete Rollback() lifecycle - pkg/datasource/sql/conn_xa.go: Fix nil pointer issue, add SQL error reporting, update prepareTime - pkg/datasource/sql/tx_xa_test.go: Add comprehensive unit tests (11 tests covering all scenarios) **Test Results:** - All existing tests pass - 11 new tests added and passing - Build successful with no errors Co-authored-by: Oracle Review <architectural-review>
e010686 to
0aa178b
Compare
There was a problem hiding this comment.
Pull request overview
This PR fixes key gaps in Seata-Go XA mode by implementing phase-1 XA commit handling (commitOnXA()), wiring XA-capable connections into the transaction object, and delegating XA LockQuery() to the RM remoting layer so global lock checks are actually performed.
Changes:
- Implement XA phase-1 commit (
XA END+XA PREPARE) and reporting to TC; improve XA rollback flow and reporting. - Fix XA
LockQuery()by delegating tormRemoting.LockQuery. - Introduce
XAConnectiononTxand wireXAConnintoBeginTx, plus add/adjust tests for XA behavior.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| pkg/datasource/sql/xa_resource_manager.go | Makes XA LockQuery() actually query TC via rmRemoting. |
| pkg/datasource/sql/tx_xa.go | Implements XA phase-1 commit and improves rollback/reporting behavior. |
| pkg/datasource/sql/tx.go | Adds XAConnection plumbing and includes BranchType in branch reporting. |
| pkg/datasource/sql/conn_xa.go | Wires XAConn into Tx creation and adjusts auto-commit execution flow. |
| pkg/datasource/sql/tx_xa_test.go | Adds unit tests for commitOnXA, rollback/reporting, and option wiring. |
| pkg/datasource/sql/conn_xa_test.go | Adds test helpers and new tests covering auto-commit phase-1 reporting and rollback behavior. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
新更改:已修复 XA 分支生命周期:移除了 BeginTx 中错误的本地物理事务启动(代码先调用了数据库驱动的 Begin/BeginTx,这会在连接上发出普通事务),改为仅由 XA 流程驱动(XA 应该只走 XA 生命周期,不该先开一层普通事务,否则就会变成“两套事务状态混在一起”,容易出现状态不一致),并在 Rollback 缺失 xaConn 时显式报错以避免 RM/TC 状态不一致。 |
AlexStocks
left a comment
There was a problem hiding this comment.
[P0] conn_xa.go:54-64 xaBranchTx 空实现违反 XA 语义,Commit/Rollback 返回 nil 但不执行任何操作,可能导致数据不一致。建议返回明确错误或记录警告日志。
[P1] conn_xa.go:143 newTx 参数传递 branchTx 作为 originTx,branchTx 是空实现,后续调用 tx.target.Rollback() 会无操作。建议明确注释说明这是有意设计。
[P2] tx_xa.go:20-23 Import 顺序违反 Go 规范,标准库和第三方库应分组。
|
新更改:把 XA 的那个“占位事务”改成了误用就直接报错,避免表面成功、实际没执行的问题;顺手把 LockQuery 的测试补齐了,确保远程返回什么这里就按什么走。 |
Summary
Fix XA mode implementation - implements
commitOnXA()andLockQuery()as reported in Issue #1060.Problem
The XA mode in seata-go had two critical unimplemented methods:
commitOnXA()- Empty implementation returningnil, causing XA transactions to not execute properlyLockQuery()- Always returned(false, nil)without checking global locksThis caused XA mode to be unusable in production environments.
Solution
1.
pkg/datasource/sql/tx_xa.goImplements
commitOnXA()to:xaConn.Commit()2.
pkg/datasource/sql/xa_resource_manager.goFixes
LockQuery()to delegate tormRemoting.LockQuery()for actual global lock checking against TC3.
pkg/datasource/sql/tx.goAdds
xaConnfield toTxstruct to support XA transaction coordination4.
pkg/datasource/sql/conn_xa.goSets
xaConnreference inBeginTx()to enable transaction coordinationHow It Works
Seata XA uses two-phase commit:
Phase 1 (Application):
Phase 2 (TC):
5. TC decides to commit → calls BranchCommit()
6. BranchCommit() → XA COMMIT (final step)
This PR implements Phase 1. Phase 2 was already implemented in
XAResourceManager.BranchCommit().Testing
go build ./...go vet ./pkg/datasource/sql/...go test ./pkg/datasource/sql/...Related Issues