mirror of https://github.com/go-gorm/gorm.git
SkipDefaultTransaction skip CreateInBatches transaction
This commit is contained in:
parent
59fa07953c
commit
4a15540504
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Reference in New Issue