revert: disable savepoint in prepare stmt

This commit is contained in:
a631807682 2023-02-20 13:42:28 +08:00
parent a3f8c94efe
commit 20275fa194
No known key found for this signature in database
GPG Key ID: 137D1D75522168AB
3 changed files with 3 additions and 6 deletions

View File

@ -617,8 +617,7 @@ func (db *DB) Transaction(fc func(tx *DB) error, opts ...*sql.TxOptions) (err er
if committer, ok := db.Statement.ConnPool.(TxCommitter); ok && committer != nil { if committer, ok := db.Statement.ConnPool.(TxCommitter); ok && committer != nil {
// nested transaction // nested transaction
if !db.DisableNestedTransaction && (!db.PrepareStmt || if !db.DisableNestedTransaction && !db.PrepareStmt {
(db.PrepareStmt && !db.DisablePrepareNestedTransaction)) {
err = db.SavePoint(fmt.Sprintf("sp%p", fc)).Error err = db.SavePoint(fmt.Sprintf("sp%p", fc)).Error
if err != nil { if err != nil {
return return

View File

@ -41,8 +41,6 @@ type Config struct {
IgnoreRelationshipsWhenMigrating bool IgnoreRelationshipsWhenMigrating bool
// DisableNestedTransaction disable nested transaction // DisableNestedTransaction disable nested transaction
DisableNestedTransaction bool DisableNestedTransaction bool
// DisablePrepareNestedTransaction disable nested transaction in prepare statement
DisablePrepareNestedTransaction bool
// AllowGlobalUpdate allow global update // AllowGlobalUpdate allow global update
AllowGlobalUpdate bool AllowGlobalUpdate bool
// QueryFields executes the SQL query with all fields of the table // QueryFields executes the SQL query with all fields of the table

View File

@ -62,7 +62,7 @@ func TestPreparedStmt(t *testing.T) {
tx1.Transaction(func(tx2 *gorm.DB) error { tx1.Transaction(func(tx2 *gorm.DB) error {
tx2.Create(&users[1]) tx2.Create(&users[1])
return errors.New("rollback user2") // Rollback user3 return errors.New("rollback user2") // not support prepare savepoint
}) })
tx1.Transaction(func(tx2 *gorm.DB) error { tx1.Transaction(func(tx2 *gorm.DB) error {
@ -76,7 +76,7 @@ func TestPreparedStmt(t *testing.T) {
var psUsers []User var psUsers []User
err = tx.Where("name like ?", "prepared_stmt_transaction%").Find(&psUsers).Error err = tx.Where("name like ?", "prepared_stmt_transaction%").Find(&psUsers).Error
AssertEqual(t, nil, err) AssertEqual(t, nil, err)
AssertEqual(t, 2, len(psUsers)) AssertEqual(t, 3, len(psUsers))
} }
func TestPreparedStmtFromTransaction(t *testing.T) { func TestPreparedStmtFromTransaction(t *testing.T) {