mirror of https://github.com/go-gorm/gorm.git
avoid starting a transaction when performing only one insert operation in CreateInBatches function (#6174)
This commit is contained in:
parent
1a7ea98ac5
commit
5d1cdfef2e
|
@ -35,9 +35,10 @@ func (db *DB) CreateInBatches(value interface{}, batchSize int) (tx *DB) {
|
||||||
var rowsAffected int64
|
var rowsAffected int64
|
||||||
tx = db.getInstance()
|
tx = db.getInstance()
|
||||||
|
|
||||||
callFc := func(tx *DB) error {
|
|
||||||
// the reflection length judgment of the optimized value
|
// the reflection length judgment of the optimized value
|
||||||
reflectLen := reflectValue.Len()
|
reflectLen := reflectValue.Len()
|
||||||
|
|
||||||
|
callFc := func(tx *DB) error {
|
||||||
for i := 0; i < reflectLen; i += batchSize {
|
for i := 0; i < reflectLen; i += batchSize {
|
||||||
ends := i + batchSize
|
ends := i + batchSize
|
||||||
if ends > reflectLen {
|
if ends > reflectLen {
|
||||||
|
@ -55,7 +56,7 @@ func (db *DB) CreateInBatches(value interface{}, batchSize int) (tx *DB) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if tx.SkipDefaultTransaction {
|
if tx.SkipDefaultTransaction || reflectLen <= batchSize {
|
||||||
tx.AddError(callFc(tx.Session(&Session{})))
|
tx.AddError(callFc(tx.Session(&Session{})))
|
||||||
} else {
|
} else {
|
||||||
tx.AddError(tx.Transaction(callFc))
|
tx.AddError(tx.Transaction(callFc))
|
||||||
|
|
Loading…
Reference in New Issue