From 740f2be4535f7156752a5e6ce4bc94db59948a10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=9C=E6=96=B9=E4=B8=8A=E4=BA=BA?= Date: Wed, 31 May 2023 19:21:51 +0800 Subject: [PATCH] fix: begin transaction fail, rollback panic (#6365) --- prepare_stmt.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/prepare_stmt.go b/prepare_stmt.go index e09fe814..4b3551c6 100644 --- a/prepare_stmt.go +++ b/prepare_stmt.go @@ -3,6 +3,7 @@ package gorm import ( "context" "database/sql" + "reflect" "sync" ) @@ -163,14 +164,14 @@ type PreparedStmtTX struct { } func (tx *PreparedStmtTX) Commit() error { - if tx.Tx != nil { + if tx.Tx != nil && !reflect.ValueOf(tx.Tx).IsNil() { return tx.Tx.Commit() } return ErrInvalidTransaction } func (tx *PreparedStmtTX) Rollback() error { - if tx.Tx != nil { + if tx.Tx != nil && !reflect.ValueOf(tx.Tx).IsNil() { return tx.Tx.Rollback() } return ErrInvalidTransaction