revert(transaction_api): remove savepoint name pool,meaningless
Change-Id: I84aa9924fc54612005a81c83d66fdf8968ee56ad Signed-off-by: 王柳洋 <wangliuyang.520@bytedance.com>
This commit is contained in:
parent
7bd0e7054c
commit
2659a60042
@ -6,8 +6,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
|
||||||
"sync/atomic"
|
|
||||||
|
|
||||||
"gorm.io/gorm/clause"
|
"gorm.io/gorm/clause"
|
||||||
"gorm.io/gorm/logger"
|
"gorm.io/gorm/logger"
|
||||||
@ -612,15 +610,6 @@ func (db *DB) Connection(fc func(tx *DB) error) (err error) {
|
|||||||
return fc(tx)
|
return fc(tx)
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
|
||||||
savepointIdx int64
|
|
||||||
savepointNamePool = &sync.Pool{
|
|
||||||
New: func() interface{} {
|
|
||||||
return fmt.Sprintf("gorm_%d", atomic.AddInt64(&savepointIdx, 1))
|
|
||||||
},
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
// Transaction start a transaction as a block, return error will rollback, otherwise to commit. Transaction executes an
|
// Transaction start a transaction as a block, return error will rollback, otherwise to commit. Transaction executes an
|
||||||
// arbitrary number of commands in fc within a transaction. On success the changes are committed; if an error occurs
|
// arbitrary number of commands in fc within a transaction. On success the changes are committed; if an error occurs
|
||||||
// they are rolled back.
|
// they are rolled back.
|
||||||
@ -630,16 +619,14 @@ 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 {
|
if !db.DisableNestedTransaction {
|
||||||
poolName := savepointNamePool.Get()
|
err = db.SavePoint(fmt.Sprintf("sp%p", fc)).Error
|
||||||
defer savepointNamePool.Put(poolName)
|
|
||||||
err = db.SavePoint(poolName.(string)).Error
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer func() {
|
defer func() {
|
||||||
// Make sure to rollback when panic, Block error or Commit error
|
// Make sure to rollback when panic, Block error or Commit error
|
||||||
if panicked || err != nil {
|
if panicked || err != nil {
|
||||||
db.RollbackTo(poolName.(string))
|
db.RollbackTo(fmt.Sprintf("sp%p", fc))
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user