From 4e24b4c36e9c364b41524c7e51cc284bed5cdb8a Mon Sep 17 00:00:00 2001 From: brad-defined <77982333+brad-defined@users.noreply.github.com> Date: Mon, 21 Apr 2025 06:42:34 -0400 Subject: [PATCH] Transaction Commit/Rollback returns conn's cached error, if present (#1691) If a transaction connection has a cached error, return it instead of ErrInvalidConn during Commit/Rollback operations. Fix #1690 --- AUTHORS | 2 ++ transaction.go | 18 ++++++++++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/AUTHORS b/AUTHORS index a261819f2..ec346e203 100644 --- a/AUTHORS +++ b/AUTHORS @@ -25,6 +25,7 @@ Asta Xie B Lamarche Bes Dollma Bogdan Constantinescu +Brad Higgins Brian Hendriks Bulat Gaifullin Caine Jette @@ -134,6 +135,7 @@ Ziheng Lyu Barracuda Networks, Inc. Counting Ltd. +Defined Networking Inc. DigitalOcean Inc. Dolthub Inc. dyves labs AG diff --git a/transaction.go b/transaction.go index 4a4b61001..8c502f49e 100644 --- a/transaction.go +++ b/transaction.go @@ -13,18 +13,32 @@ type mysqlTx struct { } func (tx *mysqlTx) Commit() (err error) { - if tx.mc == nil || tx.mc.closed.Load() { + if tx.mc == nil { return ErrInvalidConn } + if tx.mc.closed.Load() { + err = tx.mc.error() + if err == nil { + err = ErrInvalidConn + } + return + } err = tx.mc.exec("COMMIT") tx.mc = nil return } func (tx *mysqlTx) Rollback() (err error) { - if tx.mc == nil || tx.mc.closed.Load() { + if tx.mc == nil { return ErrInvalidConn } + if tx.mc.closed.Load() { + err = tx.mc.error() + if err == nil { + err = ErrInvalidConn + } + return + } err = tx.mc.exec("ROLLBACK") tx.mc = nil return