You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@seata.apache.org by "YuZhangLarry (via GitHub)" <gi...@apache.org> on 2026/03/01 16:31:08 UTC
[PR] Feature/xa branch reuse autocommit [incubator-seata-go]
YuZhangLarry opened a new pull request, #1052:
URL: https://github.com/apache/incubator-seata-go/pull/1052
- [x] I have registered the PR [changes](https://github.com/apache/incubator-seata-go/tree/master/changes).
**What this PR does**:
Support XA branch reuse in autoCommit mode. When autoCommit is enabled, each SQL statement was creating a new XA branch, which could lead to "busy buffer" issues (#904). This
change allows multiple SQL statements within the same transaction to reuse the same XA branch.
**Which issue(s) this PR fixes**:
Fixes #904
**Special notes for your reviewer**:
Key changes:
- Add `xaRegistry` to manage XA branch lifecycle and support reuse
- `BeginTx`: save original autoCommit state to GlobalLockRequire
- `createNewTxOnExecIfNeed`: implement XA branch reuse logic
- `Commit`: skip actual XA commit in autoCommit mode (defer to TC Phase2)
- `Rollback`: add conditional rollback and registry cleanup
- `BuildExecutor`: filter out undoLogSQLHook for XA mode
- `undoLogSQLHook`: skip undo log generation for XA mode
- `Tx.Rollback`: handle nil target case for XA mode
**Does this PR introduce a user-facing change?**:
```release-note
Support XA branch reuse in autoCommit mode to avoid "busy buffer" issues
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For additional commands, e-mail: notifications-help@seata.apache.org
Re: [PR] feat: XA mode 'busy buffer' and 'bad connection' errors causing rollbacks [incubator-seata-go]
Posted by "thunguo (via GitHub)" <gi...@apache.org>.
thunguo commented on PR #1052:
URL: https://github.com/apache/incubator-seata-go/pull/1052#issuecomment-4318420957
Add some unit tests.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For additional commands, e-mail: notifications-help@seata.apache.org
Re: [PR] feat: XA mode 'busy buffer' and 'bad connection' errors causing rollbacks [incubator-seata-go]
Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #1052:
URL: https://github.com/apache/incubator-seata-go/pull/1052#discussion_r2986629967
##########
pkg/datasource/sql/conn_xa.go:
##########
@@ -337,6 +392,12 @@ func (c *XAConn) rollbackErrorHandle() error {
}
func (c *XAConn) Commit(ctx context.Context) error {
+ // If this XA branch was created in autoCommit mode (multi-statement transaction),
+ // don't do the actual XA commit here. The TC will handle it in Phase 2.
+ if c.txCtx.IsAutoCommitXABranch {
Review Comment:
`IsAutoCommitXABranch` causes an early return here, but the XA branch reuse registry (`xaConnsByXID`) is not cleaned up on the success path for auto-commit multi-statement transactions. Since `UnregisterXABranch` is only called in `Rollback` and the non-autoCommit prepare path, this can leak entries indefinitely. Ensure the xid->conn mapping is removed when the global transaction finishes (e.g., during phase2 `BranchCommit`/`BranchRollback`, or in `XaCommit`/`XaRollback` after the RM completes).
```suggestion
if c.txCtx.IsAutoCommitXABranch {
// The global transaction is finishing; clean up the XA branch reuse registry entry.
c.res.UnregisterXABranch(c.txCtx.XID)
```
##########
test/xa_issue904_test.go:
##########
@@ -0,0 +1,127 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package test
+
+import (
+ "context"
+ "database/sql"
+ "fmt"
+ "os"
+ "path/filepath"
+ "testing"
+ "time"
+
+ _ "github.com/go-sql-driver/mysql"
+ "seata.apache.org/seata-go/v2/pkg/client"
+ "seata.apache.org/seata-go/v2/pkg/tm"
+)
+
+func init() {
+ // 设置配置文件路径
+ absPath, _ := filepath.Abs("../testdata/conf/seatago.yml")
+ os.Setenv("SEATA_GO_CONFIG_PATH", absPath)
+}
+
+// TestXAIssue904 复现 issue #904
+// XA 模式下 SELECT FOR UPDATE 后 UPDATE 报 busy buffer / bad connection
+func TestXAIssue904(t *testing.T) {
+ // MySQL 连接信息
+ const mysqlDSN = "root:1234@tcp(127.0.0.1:3306)/seata_test?charset=utf8mb4"
+
+ t.Log("=== 测试 issue #904: XA 模式 SELECT FOR UPDATE 后 UPDATE ===")
+
+ // 初始化 Seata 客户端
+ client.Init()
+ t.Log("Seata client initialized")
+
+ // 创建 XA 数据源
+ db, err := sql.Open("seata-xa-mysql", mysqlDSN)
+ if err != nil {
+ t.Fatalf("Failed to open database: %v", err)
Review Comment:
This test hard-codes a local MySQL DSN and assumes a running Seata server/config, which will make `go test ./...` fail or hang in CI and for most dev environments. Convert this into an opt-in integration test (e.g., guard with an env var and `t.Skip` when not set), and read DSN/config from env instead of embedding credentials/hostnames in the test.
##########
pkg/datasource/sql/conn_xa.go:
##########
@@ -198,30 +209,64 @@ func (c *XAConn) createNewTxOnExecIfNeed(ctx context.Context, f func() (types.Ex
}()
currentAutoCommit := c.autoCommit
+
+ // For global transactions in autoCommit mode, create/reuse XA branch
if c.txCtx.TransactionMode != types.Local && tm.IsGlobalTx(ctx) && c.autoCommit {
- tx, err = c.BeginTx(ctx, driver.TxOptions{Isolation: driver.IsolationLevel(gosql.LevelDefault)})
- if err != nil {
- return nil, err
+ // Check if we already have an active XA branch for this transaction
+ heldConn := c.res.GetXABranch(xid)
+ if heldConn != nil && heldConn.xaActive && heldConn.txCtx.XID == xid {
+ if heldConn != c {
+ // Delegate to the connection that holds the XA branch
+ return heldConn.createNewTxOnExecIfNeed(ctx, f)
+ }
+ // Current connection already has the XA branch, execute SQL directly
+ // Skip creating a new branch
+ } else {
+ // Create new XA branch
Review Comment:
The delegation path for XA branch reuse is incorrect: when `heldConn != c`, you call `heldConn.createNewTxOnExecIfNeed(ctx, f)` but `f` is a closure created in `QueryContext`/`ExecContext` that executes on the original receiver `c.Conn`, not on `heldConn.Conn`. This means the SQL still runs on the wrong physical connection while the XA branch is held on `heldConn`, defeating branch reuse and potentially breaking transaction semantics. Restructure this so the SQL execution is performed on `heldConn` (e.g., make `createNewTxOnExecIfNeed` accept a callback that receives the connection to use, or handle the `heldConn` selection before constructing the callback).
```suggestion
// Check if we already have an active XA branch for this transaction on the *same* connection
heldConn := c.res.GetXABranch(xid)
if heldConn != nil && heldConn == c && heldConn.xaActive && heldConn.txCtx.XID == xid {
// Current connection already has the XA branch, execute SQL directly
// Skip creating a new branch
} else {
// Create new XA branch bound to the current connection
```
##########
pkg/datasource/sql/conn_xa.go:
##########
@@ -172,23 +177,29 @@ func (c *XAConn) createOnceTxContext(ctx context.Context) bool {
c.txCtx.ResourceID = c.res.resourceID
c.txCtx.XID = tm.GetXID(ctx)
c.txCtx.TransactionMode = types.XAMode
- c.txCtx.GlobalLockRequire = true
+ c.txCtx.IsAutoCommitXABranch = true
}
return onceTx
}
func (c *XAConn) createNewTxOnExecIfNeed(ctx context.Context, f func() (types.ExecResult, error)) (types.ExecResult, error) {
var (
- tx driver.Tx
- err error
+ tx driver.Tx
+ err error
+ xaRollbacked bool // Track if XA rollback was already done to avoid duplicate rollback
)
+ xid := tm.GetXID(ctx)
+
defer func() {
recoverErr := recover()
- if err != nil || recoverErr != nil {
- log.Errorf("conn at rollback error:%v or recoverErr:%v", err, recoverErr)
- if c.tx != nil {
+ // Check if error is ErrSkip - don't rollback for this special error
+ isErrSkip := err != nil && (err == driver.ErrSkip || err.Error() == "driver: skip fast-path; continue as if unimplemented")
Review Comment:
`driver.ErrSkip` handling relies partly on comparing `err.Error()` to a hard-coded string. This is brittle and can break if the error gets wrapped or the message changes. Prefer `errors.Is(err, driver.ErrSkip)` (or a direct equality check if you know the driver returns the sentinel) and remove the string comparison.
```suggestion
isErrSkip := err != nil && errors.Is(err, driver.ErrSkip)
```
##########
pkg/datasource/sql/tx.go:
##########
@@ -135,6 +135,12 @@ func (tx *Tx) Rollback() error {
}
}
+ // In XA mode, target might be nil (set with withOriginTx(nil))
+ // The XA transaction is managed separately, so just return nil
+ if tx.target == nil {
+ return nil
Review Comment:
`Rollback()` now silently succeeds whenever `tx.target` is nil. Since a nil target is expected only for XA mode (see `withOriginTx(nil)`), this should be guarded (e.g., only allow nil when `tx.tranCtx != nil && tx.tranCtx.TransactionMode == types.XAMode`) and otherwise return an error to avoid masking unexpected driver/initialization bugs.
```suggestion
// In XA mode, target might be nil (set with withOriginTx(nil)).
// Only allow a nil target when we are explicitly in XA mode; otherwise,
// treat it as an error to avoid masking unexpected driver/initialization bugs.
if tx.target == nil {
if tx.tranCtx != nil && tx.tranCtx.TransactionMode == types.XAMode {
// XA transaction is managed separately.
return nil
}
return fmt.Errorf("sql.Tx Rollback: nil underlying transaction in non-XA mode")
```
##########
pkg/datasource/sql/exec/executor.go:
##########
@@ -58,6 +58,15 @@ func BuildExecutor(dbType types.DBType, transactionMode types.TransactionMode, q
hooks = append(hooks, commonHook...)
hooks = append(hooks, hookSolts[parseContext.SQLType]...)
+ // For XA mode, use a plain executor without AT-specific hooks
+ // XA transactions don't need undo logs - they use the two-phase commit protocol managed by TC
+ // Note: undo_log_hook.go already handles skipping undo log generation for XA mode
+ if transactionMode == types.XAMode {
+ e := &BaseExecutor{}
+ e.Interceptors(hooks)
+ return e, nil
+ }
Review Comment:
The new XA-mode branch in `BuildExecutor` isn’t covered by tests (current `executor_test.go` only exercises AT mode). Add a test case for `transactionMode == types.XAMode` to ensure a `BaseExecutor` is returned and interceptors are still applied as expected.
##########
test/xa_issue904_test.go:
##########
@@ -0,0 +1,127 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package test
+
+import (
+ "context"
+ "database/sql"
+ "fmt"
+ "os"
+ "path/filepath"
+ "testing"
+ "time"
+
+ _ "github.com/go-sql-driver/mysql"
+ "seata.apache.org/seata-go/v2/pkg/client"
+ "seata.apache.org/seata-go/v2/pkg/tm"
+)
+
+func init() {
+ // 设置配置文件路径
+ absPath, _ := filepath.Abs("../testdata/conf/seatago.yml")
+ os.Setenv("SEATA_GO_CONFIG_PATH", absPath)
+}
+
+// TestXAIssue904 复现 issue #904
+// XA 模式下 SELECT FOR UPDATE 后 UPDATE 报 busy buffer / bad connection
+func TestXAIssue904(t *testing.T) {
+ // MySQL 连接信息
+ const mysqlDSN = "root:1234@tcp(127.0.0.1:3306)/seata_test?charset=utf8mb4"
+
+ t.Log("=== 测试 issue #904: XA 模式 SELECT FOR UPDATE 后 UPDATE ===")
+
+ // 初始化 Seata 客户端
+ client.Init()
+ t.Log("Seata client initialized")
+
+ // 创建 XA 数据源
+ db, err := sql.Open("seata-xa-mysql", mysqlDSN)
+ if err != nil {
+ t.Fatalf("Failed to open database: %v", err)
+ }
+ defer db.Close()
+
+ t.Log("XA 数据源创建成功")
+
+ // 重置测试数据
+ resetTestData(t, mysqlDSN)
+
+ // 执行 XA 事务测试
+ t.Run("SELECT_FOR_UPDATE_then_UPDATE", func(t *testing.T) {
+ err = tm.WithGlobalTx(context.Background(), &tm.GtxConfig{
+ Name: "test-xa-issue904",
+ Timeout: 60 * time.Second,
+ }, func(ctx context.Context) error {
+ // 第一步:查询 (带 FOR UPDATE)
+ var money int
+ err := db.QueryRowContext(ctx,
+ "SELECT money FROM account_tbl WHERE user_id = ? FOR UPDATE", "user1").
+ Scan(&money)
+ if err != nil {
+ return fmt.Errorf("query failed: %w", err)
+ }
+ t.Logf("Step 1: 查询成功, money = %d", money)
+
+ // 第二步:更新
+ newBalance := money - 10
+ _, err = db.ExecContext(ctx,
+ "UPDATE account_tbl SET money = ? WHERE user_id = ?", newBalance, "user1")
+ if err != nil {
+ return fmt.Errorf("update failed: %w", err)
+ }
+ t.Logf("Step 2: 更新成功, new balance = %d", newBalance)
+ return nil
+ })
+
+ if err != nil {
+ t.Errorf("❌ 事务失败: %v", err)
+ } else {
+ t.Log("✅ 事务成功")
+ }
+ })
+}
+
+func resetTestData(t *testing.T, dsn string) {
+ nativeDB, err := sql.Open("mysql", dsn)
+ if err != nil {
+ t.Fatalf("Failed to open native db: %v", err)
+ }
+ defer nativeDB.Close()
+
+ // 创建表
+ _, err = nativeDB.Exec(`
+ CREATE TABLE IF NOT EXISTS account_tbl (
+ id int(11) NOT NULL AUTO_INCREMENT,
+ user_id varchar(255) DEFAULT NULL,
+ money int(11) DEFAULT 0,
+ PRIMARY KEY (id)
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8
+ `)
+ if err != nil {
+ t.Logf("Warning: create table failed: %v", err)
+ }
+
+ // 重置数据
+ nativeDB.Exec("DELETE FROM account_tbl WHERE user_id = 'user1'")
+ _, err = nativeDB.Exec("INSERT INTO account_tbl (user_id, money) VALUES ('user1', 100)")
+ if err != nil {
+ t.Logf("Warning: insert data failed: %v", err)
Review Comment:
`resetTestData` ignores errors from the DELETE statement, and downgrades CREATE TABLE / INSERT failures to warnings, which can lead to false positives (the test continues even when setup failed). Make the setup fail fast by checking and returning/fatal-ing on all SQL execution errors.
```suggestion
t.Fatalf("Failed to create table account_tbl: %v", err)
}
// 重置数据
_, err = nativeDB.Exec("DELETE FROM account_tbl WHERE user_id = 'user1'")
if err != nil {
t.Fatalf("Failed to delete existing test data: %v", err)
}
_, err = nativeDB.Exec("INSERT INTO account_tbl (user_id, money) VALUES ('user1', 100)")
if err != nil {
t.Fatalf("Failed to insert test data: %v", err)
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For additional commands, e-mail: notifications-help@seata.apache.org
Re: [PR] Feature/xa branch reuse autocommit [incubator-seata-go]
Posted by "thunguo (via GitHub)" <gi...@apache.org>.
thunguo commented on PR #1052:
URL: https://github.com/apache/incubator-seata-go/pull/1052#issuecomment-3999644751
Fix CI problem.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For additional commands, e-mail: notifications-help@seata.apache.org
Re: [PR] fix: XA mode 'busy buffer' and 'bad connection' errors causing rollbacks [incubator-seata-go]
Posted by "Ethan-Xingyue (via GitHub)" <gi...@apache.org>.
Ethan-Xingyue commented on PR #1052:
URL: https://github.com/apache/incubator-seata-go/pull/1052#issuecomment-4053645268
Please fix the CI issue.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For additional commands, e-mail: notifications-help@seata.apache.org
Re: [PR] fix: XA mode 'busy buffer' and 'bad connection' errors causing rollbacks [incubator-seata-go]
Posted by "YuZhangLarry (via GitHub)" <gi...@apache.org>.
YuZhangLarry commented on code in PR #1052:
URL: https://github.com/apache/incubator-seata-go/pull/1052#discussion_r2961062840
##########
test/xa_issue904_test.go:
##########
@@ -0,0 +1,127 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package test
Review Comment:
OK, I'll be careful.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For additional commands, e-mail: notifications-help@seata.apache.org
Re: [PR] Feature/xa branch reuse autocommit [incubator-seata-go]
Posted by "thunguo (via GitHub)" <gi...@apache.org>.
thunguo commented on PR #1052:
URL: https://github.com/apache/incubator-seata-go/pull/1052#issuecomment-3982248912
Change the PR title to "Feat: <pull request title>" format.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For additional commands, e-mail: notifications-help@seata.apache.org
Re: [PR] feat: XA mode 'busy buffer' and 'bad connection' errors causing rollbacks [incubator-seata-go]
Posted by "thunguo (via GitHub)" <gi...@apache.org>.
thunguo commented on PR #1052:
URL: https://github.com/apache/incubator-seata-go/pull/1052#issuecomment-4265550209
The unit test part of CI failed, fix it.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For additional commands, e-mail: notifications-help@seata.apache.org
Re: [PR] Feature/xa branch reuse autocommit [incubator-seata-go]
Posted by "thunguo (via GitHub)" <gi...@apache.org>.
thunguo commented on code in PR #1052:
URL: https://github.com/apache/incubator-seata-go/pull/1052#discussion_r2870692292
##########
pkg/datasource/sql/xa_registry.go:
##########
@@ -0,0 +1,192 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package sql
Review Comment:
We need to consider whether the registry is truly necessary. My understanding is that as long as there is an active XA branch on the current connection, it can be reused. Perhaps it would be better to check this situation?
##########
pkg/datasource/sql/conn_xa.go:
##########
@@ -127,9 +139,15 @@ func (c *XAConn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx,
c.txCtx.ResourceID = c.res.resourceID
c.txCtx.XID = tm.GetXID(ctx)
c.txCtx.TransactionMode = types.XAMode
+ // Store the original autoCommit state for later use in commit logic
+ c.txCtx.GlobalLockRequire = wasAutoCommit
Review Comment:
Is there any semantic confusion here? Would defining a new field like `IsAutoCommitXABranch` be better?
##########
pkg/datasource/sql/exec/executor.go:
##########
@@ -58,6 +59,29 @@ func BuildExecutor(dbType types.DBType, transactionMode types.TransactionMode, q
hooks = append(hooks, commonHook...)
hooks = append(hooks, hookSolts[parseContext.SQLType]...)
+ // For XA mode, use a plain executor without AT-specific hooks (like undo log generation)
+ // XA transactions don't need undo logs - they use the two-phase commit protocol managed by TC
+ if transactionMode == types.XAMode {
Review Comment:
Could you double-check if this change is necessary? The `undo_log_hook.go` file handles this conditional branch, meaning the hook should occur earlier than the logic in the executor. It's likely that this part won't actually be executed. I understand that the current changes in `undo_log_hook.go` are sufficient.
##########
pkg/datasource/sql/conn_xa.go:
##########
@@ -184,45 +202,110 @@ func (c *XAConn) createNewTxOnExecIfNeed(ctx context.Context, f func() (types.Ex
err error
)
+ xid := tm.GetXID(ctx)
+ log.Infof("[XA-DEBUG] createNewTxOnExecIfNeed called, xid: %s, autoCommit: %v, txCtx.TransactionMode: %v, txCtx.XID: %s",
+ xid, c.autoCommit, c.txCtx.TransactionMode, c.txCtx.XID)
+
defer func() {
recoverErr := recover()
- if err != nil || recoverErr != nil {
- log.Errorf("conn at rollback error:%v or recoverErr:%v", err, recoverErr)
+ // Check if error is ErrSkip - don't rollback for this special error
+ isErrSkip := err != nil && (err == driver.ErrSkip || err.Error() == "driver: skip fast-path; continue as if unimplemented")
+
+ if (err != nil && !isErrSkip) || recoverErr != nil {
+ log.Errorf("[XA-DEBUG] defer triggered, err: %v, recoverErr: %v", err, recoverErr)
+ // Don't try to rollback if tx is nil
if c.tx != nil {
+ log.Infof("[XA-DEBUG] calling c.tx.Rollback() in defer")
rollbackErr := c.tx.Rollback()
if rollbackErr != nil {
log.Errorf("conn at rollback error:%v", rollbackErr)
}
}
+ } else {
+ log.Infof("[XA-DEBUG] defer: no error or ErrSkip, skipping rollback")
}
}()
currentAutoCommit := c.autoCommit
+ registry := getXARegistry()
+
+ // For global transactions in autoCommit mode, create/reuse XA branch
if c.txCtx.TransactionMode != types.Local && tm.IsGlobalTx(ctx) && c.autoCommit {
+ // Check if we already have an active XA branch for this transaction
+ if registry.canReuse(xid) {
+ // Reuse existing XA branch - execute SQL directly
+ log.Infof("Reusing existing XA branch for xid: %s", xid)
+ ret, err := f()
+ if err != nil {
+ // On error (but not ErrSkip), rollback the entire branch
+ isErrSkip := err == driver.ErrSkip || err.Error() == "driver: skip fast-path; continue as if unimplemented"
+ if !isErrSkip && c.xaActive {
+ if rollbackErr := c.Rollback(ctx); rollbackErr != nil {
Review Comment:
`c.Rollback(ctx)` performs an XA-level rollback (XA END + XA ROLLBACK), but does not set `c.tx` to nil. This causes the `defer` statement at the top of the function to subsequently call `c.tx.Rollback()` again, resulting in a duplicate rollback of the already terminated underlying connection. Could this potentially lead to a `bad connection`?
##########
pkg/datasource/sql/conn_xa.go:
##########
@@ -346,6 +458,11 @@ func (c *XAConn) Commit(ctx context.Context) error {
}
now := time.Now()
+
+ // Update registry state to ENDED before XA END
+ registry := getXARegistry()
+ registry.setState(c.txCtx.XID, xaStateEnded)
Review Comment:
Should the status modification operation be performed after the XA END action?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For additional commands, e-mail: notifications-help@seata.apache.org
Re: [PR] feat: XA mode 'busy buffer' and 'bad connection' errors causing rollbacks [incubator-seata-go]
Posted by "codecov-commenter (via GitHub)" <gi...@apache.org>.
codecov-commenter commented on PR #1052:
URL: https://github.com/apache/incubator-seata-go/pull/1052#issuecomment-4251430726
## [Codecov](https://app.codecov.io/gh/apache/incubator-seata-go/pull/1052?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) Report
:x: Patch coverage is `0%` with `19 lines` in your changes missing coverage. Please review.
:white_check_mark: Project coverage is 57.28%. Comparing base ([`4a684d6`](https://app.codecov.io/gh/apache/incubator-seata-go/commit/4a684d6fd969a6f8b69adae9e3e2e158b56a5a6b?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)) to head ([`1df1397`](https://app.codecov.io/gh/apache/incubator-seata-go/commit/1df1397fe67781d54291abb92bb04b0f0e64a1ee?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)).
| [Files with missing lines](https://app.codecov.io/gh/apache/incubator-seata-go/pull/1052?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Patch % | Lines |
|---|---|---|
| [pkg/datasource/sql/db.go](https://app.codecov.io/gh/apache/incubator-seata-go/pull/1052?src=pr&el=tree&filepath=pkg%2Fdatasource%2Fsql%2Fdb.go&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-cGtnL2RhdGFzb3VyY2Uvc3FsL2RiLmdv) | 0.00% | [9 Missing :warning: ](https://app.codecov.io/gh/apache/incubator-seata-go/pull/1052?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
| [pkg/datasource/sql/exec/executor.go](https://app.codecov.io/gh/apache/incubator-seata-go/pull/1052?src=pr&el=tree&filepath=pkg%2Fdatasource%2Fsql%2Fexec%2Fexecutor.go&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-cGtnL2RhdGFzb3VyY2Uvc3FsL2V4ZWMvZXhlY3V0b3IuZ28=) | 0.00% | [3 Missing and 1 partial :warning: ](https://app.codecov.io/gh/apache/incubator-seata-go/pull/1052?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
| [pkg/datasource/sql/tx.go](https://app.codecov.io/gh/apache/incubator-seata-go/pull/1052?src=pr&el=tree&filepath=pkg%2Fdatasource%2Fsql%2Ftx.go&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-cGtnL2RhdGFzb3VyY2Uvc3FsL3R4Lmdv) | 0.00% | [3 Missing and 1 partial :warning: ](https://app.codecov.io/gh/apache/incubator-seata-go/pull/1052?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
| [pkg/datasource/sql/hook/undo\_log\_hook.go](https://app.codecov.io/gh/apache/incubator-seata-go/pull/1052?src=pr&el=tree&filepath=pkg%2Fdatasource%2Fsql%2Fhook%2Fundo_log_hook.go&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-cGtnL2RhdGFzb3VyY2Uvc3FsL2hvb2svdW5kb19sb2dfaG9vay5nbw==) | 0.00% | [1 Missing and 1 partial :warning: ](https://app.codecov.io/gh/apache/incubator-seata-go/pull/1052?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
<details><summary>Additional details and impacted files</summary>
```diff
@@ Coverage Diff @@
## master #1052 +/- ##
==========================================
- Coverage 57.34% 57.28% -0.07%
==========================================
Files 267 267
Lines 17597 17616 +19
==========================================
Hits 10091 10091
- Misses 6681 6697 +16
- Partials 825 828 +3
```
</details>
[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/apache/incubator-seata-go/pull/1052?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).
<details><summary> :rocket: New features to boost your workflow: </summary>
- :snowflake: [Test Analytics](https://docs.codecov.com/docs/test-analytics): Detect flaky tests, report on failures, and find test suite problems.
</details>
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For additional commands, e-mail: notifications-help@seata.apache.org
Re: [PR] feat: XA mode 'busy buffer' and 'bad connection' errors causing rollbacks [incubator-seata-go]
Posted by "Ethan-Xingyue (via GitHub)" <gi...@apache.org>.
Ethan-Xingyue commented on PR #1052:
URL: https://github.com/apache/incubator-seata-go/pull/1052#issuecomment-4608840527
plz resolve conflicts
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For additional commands, e-mail: notifications-help@seata.apache.org
Re: [PR] feat: XA mode 'busy buffer' and 'bad connection' errors causing rollbacks [incubator-seata-go]
Posted by "flypiggyNo3 (via GitHub)" <gi...@apache.org>.
flypiggyNo3 commented on PR #1052:
URL: https://github.com/apache/incubator-seata-go/pull/1052#issuecomment-4147961563
plz reply comments
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For additional commands, e-mail: notifications-help@seata.apache.org
Re: [PR] feat: XA mode 'busy buffer' and 'bad connection' errors causing rollbacks [incubator-seata-go]
Posted by "AlexStocks (via GitHub)" <gi...@apache.org>.
AlexStocks commented on code in PR #1052:
URL: https://github.com/apache/incubator-seata-go/pull/1052#discussion_r2990515162
##########
pkg/datasource/sql/conn_xa.go:
##########
@@ -337,6 +392,12 @@ func (c *XAConn) rollbackErrorHandle() error {
}
func (c *XAConn) Commit(ctx context.Context) error {
+ // If this XA branch was created in autoCommit mode (multi-statement transaction),
+ // don't do the actual XA commit here. The TC will handle it in Phase 2.
+ if c.txCtx.IsAutoCommitXABranch {
Review Comment:
[P0] 这里直接 `return nil` 会把 autoCommit XA 分支的 phase-1 完整短路掉。当前分支里 `RegisterXABranch/GetXABranch` 只负责复用连接,没有任何后续路径会再执行 `XA END + XA PREPARE` 或向 TC 上报 `PhaseOneDone/Failed`;`tm.GlobalTransactionManager.Commit` 也只发全局提交请求,不会回调到这个本地分支。结果就是这类分支既没有 prepare,也不会从 `xaConnsByXID` 清理,事务提交路径会丢失本地分支。建议把“复用同一 XA branch”与“结束/prepare branch”的时机拆开:复用期间不要重复 `BeginTx`,但在全局事务真正结束前仍然需要一个明确的 finalize 路径来执行 END/PREPARE/BranchReport。
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For additional commands, e-mail: notifications-help@seata.apache.org
Re: [PR] Feature/xa branch reuse autocommit [incubator-seata-go]
Posted by "thunguo (via GitHub)" <gi...@apache.org>.
thunguo commented on PR #1052:
URL: https://github.com/apache/incubator-seata-go/pull/1052#issuecomment-3982262047
Fix the CI problem.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For additional commands, e-mail: notifications-help@seata.apache.org
Re: [PR] feat: XA mode 'busy buffer' and 'bad connection' errors causing rollbacks [incubator-seata-go]
Posted by "YuZhangLarry (via GitHub)" <gi...@apache.org>.
YuZhangLarry closed pull request #1052: feat: XA mode 'busy buffer' and 'bad connection' errors causing rollbacks
URL: https://github.com/apache/incubator-seata-go/pull/1052
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For additional commands, e-mail: notifications-help@seata.apache.org