Skip to content

Commit

Permalink
don't toggle ignore auto commit
Browse files Browse the repository at this point in the history
  • Loading branch information
James Cor committed Jan 17, 2025
1 parent 28d8b1d commit e0a5ed7
Showing 1 changed file with 32 additions and 24 deletions.
56 changes: 32 additions & 24 deletions sql/rowexec/transaction_iters.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ func getLockableTable(table sql.Table) (sql.Lockable, error) {
type TransactionCommittingIter struct {
childIter sql.RowIter
transactionDatabase string
shouldCommit bool
autoCommit bool
implicitCommit bool
}

func AddTransactionCommittingIter(ctx *sql.Context, qFlags *sql.QueryFlags, iter sql.RowIter) (sql.RowIter, error) {
Expand All @@ -81,19 +82,17 @@ func AddTransactionCommittingIter(ctx *sql.Context, qFlags *sql.QueryFlags, iter
return iter, nil
}

// TODO: In the future we should ensure that analyzer supports implicit commits instead of directly
// accessing autocommit here.
// cc. https://dev.mysql.com/doc/refman/8.0/en/implicit-commit.html
shouldCommit, err := plan.IsSessionAutocommit(ctx)
autoCommit, err := plan.IsSessionAutocommit(ctx)
if err != nil {
return nil, err
}

if qFlags != nil && qFlags.IsSet(sql.QFlagDDL) {
shouldCommit = true
ctx.SetIgnoreAutoCommit(false)
}
return &TransactionCommittingIter{childIter: iter, shouldCommit: shouldCommit}, nil
implicitCommit := qFlags != nil && qFlags.IsSet(sql.QFlagDDL)
return &TransactionCommittingIter{
childIter: iter,
autoCommit: autoCommit,
implicitCommit: implicitCommit,
}, nil
}

func (t *TransactionCommittingIter) Next(ctx *sql.Context) (sql.Row, error) {
Expand All @@ -110,22 +109,31 @@ func (t *TransactionCommittingIter) Close(ctx *sql.Context) error {
}

tx := ctx.GetTransaction()
commitTransaction := ((tx != nil) && !ctx.GetIgnoreAutoCommit()) && t.shouldCommit
if commitTransaction {
ts, ok := ctx.Session.(sql.TransactionSession)
if !ok {
return nil
}

ctx.GetLogger().Tracef("committing transaction %s", tx)
if err := ts.CommitTransaction(ctx, tx); err != nil {
return err
}

// Clearing out the current transaction will tell us to start a new one the next time this session queries
ctx.SetTransaction(nil)
if tx == nil {
return nil
}

if !t.implicitCommit && ctx.GetIgnoreAutoCommit() {
return nil
}

if !t.implicitCommit && !t.autoCommit {
return nil
}

ts, ok := ctx.Session.(sql.TransactionSession)
if !ok {
return nil
}

ctx.GetLogger().Tracef("committing transaction %s", tx)
if err := ts.CommitTransaction(ctx, tx); err != nil {
return err
}

// Clearing out the current transaction will tell us to start a new one the next time this session queries
ctx.SetTransaction(nil)

return nil
}

Expand Down

0 comments on commit e0a5ed7

Please sign in to comment.