mirror of https://github.com/go-gorm/gorm.git
Fix failed to create second record in same transaction, close #3060
This commit is contained in:
parent
07960fe661
commit
2c1b04a2cf
|
@ -7,7 +7,7 @@ import (
|
|||
func BeginTransaction(db *gorm.DB) {
|
||||
if tx := db.Begin(); tx.Error == nil {
|
||||
db.Statement.ConnPool = tx.Statement.ConnPool
|
||||
tx.InstanceSet("gorm:started_transaction", true)
|
||||
db.InstanceSet("gorm:started_transaction", true)
|
||||
} else {
|
||||
tx.Error = nil
|
||||
}
|
||||
|
|
|
@ -351,7 +351,7 @@ func (db *DB) Transaction(fc func(tx *DB) error, opts ...*sql.TxOptions) (err er
|
|||
}
|
||||
}()
|
||||
|
||||
err = fc(tx.Session(&Session{}))
|
||||
err = fc(tx)
|
||||
|
||||
if err == nil {
|
||||
err = tx.Commit().Error
|
||||
|
@ -364,7 +364,8 @@ func (db *DB) Transaction(fc func(tx *DB) error, opts ...*sql.TxOptions) (err er
|
|||
// Begin begins a transaction
|
||||
func (db *DB) Begin(opts ...*sql.TxOptions) *DB {
|
||||
var (
|
||||
tx = db.getInstance()
|
||||
// clone statement
|
||||
tx = db.Session(&Session{WithConditions: true, Context: db.Statement.Context})
|
||||
opt *sql.TxOptions
|
||||
err error
|
||||
)
|
||||
|
|
|
@ -351,5 +351,10 @@ func (stmt *Statement) clone() *Statement {
|
|||
newStmt.Joins[k] = j
|
||||
}
|
||||
|
||||
stmt.Settings.Range(func(k, v interface{}) bool {
|
||||
newStmt.Settings.Store(k, v)
|
||||
return true
|
||||
})
|
||||
|
||||
return newStmt
|
||||
}
|
||||
|
|
|
@ -20,6 +20,16 @@ func TestTransaction(t *testing.T) {
|
|||
t.Fatalf("Should find saved record, but got %v", err)
|
||||
}
|
||||
|
||||
user1 := *GetUser("transaction1-1", Config{})
|
||||
|
||||
if err := tx.Save(&user1).Error; err != nil {
|
||||
t.Fatalf("No error should raise, but got %v", err)
|
||||
}
|
||||
|
||||
if err := tx.First(&User{}, "name = ?", user1.Name).Error; err != nil {
|
||||
t.Fatalf("Should find saved record, but got %v", err)
|
||||
}
|
||||
|
||||
if sqlTx, ok := tx.Statement.ConnPool.(gorm.TxCommitter); !ok || sqlTx == nil {
|
||||
t.Fatalf("Should return the underlying sql.Tx")
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue