YuZhangLarry commented on code in PR #1137:
URL:
https://github.com/apache/incubator-seata-go/pull/1137#discussion_r3620270072
##########
pkg/datasource/sql/conn_xa.go:
##########
@@ -237,22 +244,51 @@ func (c *XAConn) createNewTxOnExecIfNeed(ctx
context.Context, f func() (types.Ex
// execute SQL
ret, err := f()
if err != nil {
+ // driver.ErrSkip is not a real failure: it asks database/sql
to retry this
+ // statement through the Prepare+Exec fallback path.
+ if errors.Is(err, driver.ErrSkip) {
+ // If we already opened an XA branch for this statement
(autoCommit mode),
+ // the fallback retry would run OUTSIDE the branch -
autoCommit is now false
+ // and txCtx has been reset - leaving this branch
registered-but-never-
+ // prepared (a leak) and the retried write escaping the
global transaction.
+ // Roll the branch back and surface a real
(non-ErrSkip) error so the caller
+ // fails fast instead of silently corrupting the global
transaction.
+ if tx != nil {
+ if rollbackErr := tx.Rollback(); rollbackErr !=
nil {
+ log.Errorf("failed to rollback xa
branch of :%s after ErrSkip, err:%v", c.txCtx.XID, rollbackErr)
+ }
+ xaRollbacked = true
+ return nil, fmt.Errorf("xa branch %s cannot
fall back to non-XA execution after XA START: %v", c.txCtx.XID, err)
+ }
+ // No branch opened yet - safe to let database/sql use
its fallback path.
+ return nil, err
+ }
+ // On real error, rollback the entire branch. Prefer
XATx.Rollback so the
+ // already-registered branch reports phase-1 failure to the TC;
fall back to
+ // the raw connection rollback for non-autoCommit paths.
if tx != nil {
if rollbackErr := tx.Rollback(); rollbackErr != nil {
log.Errorf("failed to rollback xa branch of
:%s, err:%v", c.txCtx.XID, rollbackErr)
}
- } else {
+ } else if c.xaActive {
if rollbackErr := c.Rollback(ctx); rollbackErr != nil {
log.Errorf("failed to rollback xa branch of
:%s, err:%v", c.txCtx.XID, rollbackErr)
}
}
+ xaRollbacked = true // Mark that rollback was handled
return nil, err
}
+ // For autoCommit mode with global transaction, commit the branch now:
+ // XA END + XA PREPARE + report phase-1 success to TC.
if tx != nil && currentAutoCommit {
- // Commit through XATx so phase-one reporting stays coupled to
driver.Tx lifecycle.
if err = tx.Commit(); err != nil {
- log.Errorf("xa connection proxy commit failure xid:%s,
err:%v", c.txCtx.XID, err)
+ log.Errorf("xa transaction commit failure xid:%s,
err:%v", c.txCtx.XID, err)
+ // XA End & Rollback
+ if rollbackErr := tx.Rollback(); rollbackErr != nil {
Review Comment:
好的,我改一下
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]