db icon indicating copy to clipboard operation
db copied to clipboard

Transactions created with TxContext don't rollback (at least using the mysql driver)

Open acwest opened this issue 3 months ago • 1 comments

I am creating a transaction with:

session.TxContext(ctx, func(txSession db.Session) error {
  err := DoSomething(txSession)
  return err
}, nil)

DoSomething internally uses InsertReturning to insert a number of rows into a table.

If I cancel the transaction while it is running, an InsertReturning call returns an error: "context canceled", so DoSomething returns the same error. The TxContext call then returns an error: "context canceled: context canceled", which is somewhat expected behaviour, but the transaction is not rolled back and all of the rows added to the table are still there.
The expected behaviour would be for the transaction to roll back and the rows to not be commited to the table.

acwest avatar Oct 01 '25 15:10 acwest

It doesn't really look like this is our fault, the mysql driver is calling cleanup on the connection, which closes it, before we have a chance to roll back or commit the transaction.

acwest avatar Oct 01 '25 17:10 acwest