Skip to content

fix: implement XA mode commitOnXA() and LockQuery()#1061

Merged
thunguo merged 16 commits intoapache:masterfrom
CAICAIIs:fix/xa-mode-implementation
Apr 22, 2026
Merged

fix: implement XA mode commitOnXA() and LockQuery()#1061
thunguo merged 16 commits intoapache:masterfrom
CAICAIIs:fix/xa-mode-implementation

Conversation

@CAICAIIs
Copy link
Copy Markdown
Contributor

@CAICAIIs CAICAIIs commented Mar 10, 2026

Summary

Fix XA mode implementation - implements commitOnXA() and LockQuery() as reported in Issue #1060.

Problem

The XA mode in seata-go had two critical unimplemented methods:

  1. commitOnXA() - Empty implementation returning nil, causing XA transactions to not execute properly
  2. LockQuery() - Always returned (false, nil) without checking global locks
    This caused XA mode to be unusable in production environments.

Solution

1. pkg/datasource/sql/tx_xa.go

Implements commitOnXA() to:

  • Check if global transaction is open
  • Execute XA END + XA PREPARE via xaConn.Commit()
  • Report branch status to TC (Phase One)

2. pkg/datasource/sql/xa_resource_manager.go

Fixes LockQuery() to delegate to rmRemoting.LockQuery() for actual global lock checking against TC

3. pkg/datasource/sql/tx.go

Adds xaConn field to Tx struct to support XA transaction coordination

4. pkg/datasource/sql/conn_xa.go

Sets xaConn reference in BeginTx() to enable transaction coordination

How It Works

Seata XA uses two-phase commit:
Phase 1 (Application):

  1. XAConn.BeginTx() → XA START + register branch
  2. Execute business SQL
  3. XATx.Commit() → commitOnXA() → XA END + XA PREPARE + report to TC

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

  • Build passes: go build ./...
  • Vet passes: go vet ./pkg/datasource/sql/...
  • Tests pass: go test ./pkg/datasource/sql/...

Related Issues

fix: implement XA mode commitOnXA() and LockQuery()

- 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
@github-actions github-actions Bot added bug Something isn't working enhancement New feature or request module/xa coding and removed bug Something isn't working enhancement New feature or request module/xa labels Mar 10, 2026
Comment thread pkg/datasource/sql/tx.go Outdated
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Mar 10, 2026

Codecov Report

❌ Patch coverage is 65.00000% with 28 lines in your changes missing coverage. Please review.
✅ Project coverage is 57.52%. Comparing base (39063bb) to head (3227b5f).

Files with missing lines Patch % Lines
pkg/datasource/sql/conn_xa.go 46.66% 13 Missing and 3 partials ⚠️
pkg/datasource/sql/tx_xa.go 70.00% 8 Missing and 4 partials ⚠️
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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@thunguo
Copy link
Copy Markdown
Contributor

thunguo commented Mar 10, 2026

Do we need to add some test cases? It seems the unit test coverage has decreased somewhat.

@github-actions github-actions Bot added bug Something isn't working enhancement New feature or request module/xa labels Mar 10, 2026
CAICAIIs added a commit to CAICAIIs/incubator-seata-go that referenced this pull request Mar 10, 2026
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>
@CAICAIIs CAICAIIs force-pushed the fix/xa-mode-implementation branch from 158ed24 to e010686 Compare March 10, 2026 11:46
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>
@CAICAIIs CAICAIIs force-pushed the fix/xa-mode-implementation branch from e010686 to 0aa178b Compare March 10, 2026 11:52
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 to rmRemoting.LockQuery.
  • Introduce XAConnection on Tx and wire XAConn into BeginTx, 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.

Comment thread pkg/datasource/sql/conn_xa.go Outdated
Comment thread pkg/datasource/sql/tx_xa.go Outdated
Comment thread pkg/datasource/sql/tx_xa_test.go Outdated
Comment thread pkg/datasource/sql/tx_xa_test.go Outdated
Comment thread pkg/datasource/sql/conn_xa_test.go Outdated
@CAICAIIs
Copy link
Copy Markdown
Contributor Author

CAICAIIs commented Mar 26, 2026

新更改:已修复 XA 分支生命周期:移除了 BeginTx 中错误的本地物理事务启动(代码先调用了数据库驱动的 Begin/BeginTx,这会在连接上发出普通事务),改为仅由 XA 流程驱动(XA 应该只走 XA 生命周期,不该先开一层普通事务,否则就会变成“两套事务状态混在一起”,容易出现状态不一致),并在 Rollback 缺失 xaConn 时显式报错以避免 RM/TC 状态不一致。

Comment thread pkg/datasource/sql/xa_resource_manager.go
Copy link
Copy Markdown
Contributor

@AlexStocks AlexStocks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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 规范,标准库和第三方库应分组。

Comment thread pkg/datasource/sql/conn_xa.go
Comment thread pkg/datasource/sql/conn_xa.go
@github-actions github-actions Bot added the rm label Mar 29, 2026
@CAICAIIs
Copy link
Copy Markdown
Contributor Author

新更改:把 XA 的那个“占位事务”改成了误用就直接报错,避免表面成功、实际没执行的问题;顺手把 LockQuery 的测试补齐了,确保远程返回什么这里就按什么走。
另外还修了测试清理,防止测试里注册的全局 mock 残留到后面的用例。

Copy link
Copy Markdown
Contributor

@thunguo thunguo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@thunguo thunguo merged commit 976e7b9 into apache:master Apr 22, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working coding enhancement New feature or request module/xa rm

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] 项目部分 TODO 或 implement me 汇总

5 participants