Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle concurrent timeout close conn #677

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions pkg/datasource/sql/conn_xa.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,16 @@ func (c *XAConn) createNewTxOnExecIfNeed(ctx context.Context, f func() (types.Ex

// execute SQL
ret, err := f()

// When c.xaActive is false, i.e, when cleanXABranchContext() has been called, it means
// this xa transaction has been terminated(db conn closed)/rollbacked/committed). No need to proceed anymore.
// This could be introduced by a concurrent timeout check located at
// pkg/database/sql/xa_resource_manager.go, xaTwoPhaseTimeoutChecker() and
// probably other similar async scenarios.
if !c.xaActive {
return nil, fmt.Errorf("xa tx has been terminated due to xaActive being false")
}

if err != nil {
// XA End & Rollback
if rollbackErr := c.Rollback(ctx); rollbackErr != nil {
Expand Down Expand Up @@ -385,16 +395,28 @@ func (c *XAConn) CloseForce() error {
}

func (c *XAConn) XaCommit(ctx context.Context, xaXid XAXid) error {
// xa tx has been either terminated(db conn closed)/rollbacked/committed already
if !c.xaActive {
return nil
}
err := c.xaResource.Commit(ctx, xaXid.String(), false)
c.releaseIfNecessary()
return err
}

func (c *XAConn) XaRollbackByBranchId(ctx context.Context, xaXid XAXid) error {
// xa tx has been either terminated(db conn closed)/rollbacked/committed already
if !c.xaActive {
return nil
}
return c.XaRollback(ctx, xaXid)
}

func (c *XAConn) XaRollback(ctx context.Context, xaXid XAXid) error {
// xa tx has been either terminated(db conn closed)/rollbacked/committed already
if !c.xaActive {
return nil
}
err := c.xaResource.Rollback(ctx, xaXid.String())
c.releaseIfNecessary()
return err
Expand Down
Loading