SkipDefaultTransaction skip CreateInBatches transaction

This commit is contained in:
Jinzhu 2021-01-18 11:43:42 +08:00
parent 59fa07953c
commit 4a15540504
2 changed files with 11 additions and 3 deletions

View File

@ -9,7 +9,7 @@ func BeginTransaction(db *gorm.DB) {
if tx := db.Begin(); tx.Error == nil {
db.Statement.ConnPool = tx.Statement.ConnPool
db.InstanceSet("gorm:started_transaction", true)
} else {
} else if tx.Error == gorm.ErrInvalidTransaction {
tx.Error = nil
}
}

View File

@ -33,7 +33,8 @@ func (db *DB) CreateInBatches(value interface{}, batchSize int) (tx *DB) {
case reflect.Slice, reflect.Array:
var rowsAffected int64
tx = db.getInstance()
tx.AddError(tx.Transaction(func(tx *DB) error {
callFc := func(tx *DB) error {
for i := 0; i < reflectValue.Len(); i += batchSize {
ends := i + batchSize
if ends > reflectValue.Len() {
@ -49,7 +50,14 @@ func (db *DB) CreateInBatches(value interface{}, batchSize int) (tx *DB) {
rowsAffected += subtx.RowsAffected
}
return nil
}))
}
if tx.SkipDefaultTransaction {
tx.AddError(callFc(tx.Session(&Session{})))
} else {
tx.AddError(tx.Transaction(callFc))
}
tx.RowsAffected = rowsAffected
default:
tx = db.getInstance()