Rollback rolls back the transaction. Rollback will return ErrTxClosed if the Tx is already closed, but is otherwise safe to call multiple times. Hence, a defer tx.Rollback() is safe even if tx.Commit() will be called first in a non-error condition.
(ctx context.Context)
| 207 | // defer tx.Rollback() is safe even if tx.Commit() will be called first in a |
| 208 | // non-error condition. |
| 209 | func (tx *dbTx) Rollback(ctx context.Context) error { |
| 210 | if tx.closed { |
| 211 | return ErrTxClosed |
| 212 | } |
| 213 | |
| 214 | _, err := tx.conn.Exec(ctx, "rollback") |
| 215 | tx.closed = true |
| 216 | if err != nil { |
| 217 | // A rollback failure leaves the connection in an undefined state |
| 218 | tx.conn.die() |
| 219 | return err |
| 220 | } |
| 221 | |
| 222 | return nil |
| 223 | } |
| 224 | |
| 225 | // Exec delegates to the underlying *Conn |
| 226 | func (tx *dbTx) Exec(ctx context.Context, sql string, arguments ...any) (commandTag pgconn.CommandTag, err error) { |