2017-01-03 16:20:28 +08:00
|
|
|
// Copyright 2016 The Xorm Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package xorm
|
|
|
|
|
|
|
|
// Begin a transaction
|
|
|
|
func (session *Session) Begin() error {
|
2017-08-22 19:39:52 +08:00
|
|
|
if session.isAutoCommit {
|
2019-06-23 23:22:43 +08:00
|
|
|
tx, err := session.DB().BeginTx(session.ctx, nil)
|
2017-01-03 16:20:28 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-08-22 19:39:52 +08:00
|
|
|
session.isAutoCommit = false
|
|
|
|
session.isCommitedOrRollbacked = false
|
|
|
|
session.tx = tx
|
2020-03-22 23:12:55 +08:00
|
|
|
|
2017-01-03 16:20:28 +08:00
|
|
|
session.saveLastSQL("BEGIN TRANSACTION")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rollback When using transaction, you can rollback if any error
|
|
|
|
func (session *Session) Rollback() error {
|
2017-08-22 19:39:52 +08:00
|
|
|
if !session.isAutoCommit && !session.isCommitedOrRollbacked {
|
2020-03-22 23:12:55 +08:00
|
|
|
session.saveLastSQL("ROLL BACK")
|
2017-08-22 19:39:52 +08:00
|
|
|
session.isCommitedOrRollbacked = true
|
2018-07-20 10:10:17 +08:00
|
|
|
session.isAutoCommit = true
|
2020-03-22 23:12:55 +08:00
|
|
|
|
2020-09-03 09:58:04 +08:00
|
|
|
return session.tx.Rollback()
|
2017-01-03 16:20:28 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Commit When using transaction, Commit will commit all operations.
|
|
|
|
func (session *Session) Commit() error {
|
2017-08-22 19:39:52 +08:00
|
|
|
if !session.isAutoCommit && !session.isCommitedOrRollbacked {
|
2017-01-03 16:20:28 +08:00
|
|
|
session.saveLastSQL("COMMIT")
|
2017-08-22 19:39:52 +08:00
|
|
|
session.isCommitedOrRollbacked = true
|
2018-07-20 10:10:17 +08:00
|
|
|
session.isAutoCommit = true
|
2020-03-22 23:12:55 +08:00
|
|
|
|
2020-09-03 09:58:04 +08:00
|
|
|
if err := session.tx.Commit(); err != nil {
|
2020-03-22 23:12:55 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// handle processors after tx committed
|
|
|
|
closureCallFunc := func(closuresPtr *[]func(interface{}), bean interface{}) {
|
|
|
|
if closuresPtr != nil {
|
|
|
|
for _, closure := range *closuresPtr {
|
|
|
|
closure(bean)
|
2017-01-03 16:20:28 +08:00
|
|
|
}
|
|
|
|
}
|
2020-03-22 23:12:55 +08:00
|
|
|
}
|
2017-01-03 16:20:28 +08:00
|
|
|
|
2020-03-22 23:12:55 +08:00
|
|
|
for bean, closuresPtr := range session.afterInsertBeans {
|
|
|
|
closureCallFunc(closuresPtr, bean)
|
2017-01-03 16:20:28 +08:00
|
|
|
|
2020-03-22 23:12:55 +08:00
|
|
|
if processor, ok := interface{}(bean).(AfterInsertProcessor); ok {
|
|
|
|
processor.AfterInsert()
|
2017-01-03 16:20:28 +08:00
|
|
|
}
|
2020-03-22 23:12:55 +08:00
|
|
|
}
|
|
|
|
for bean, closuresPtr := range session.afterUpdateBeans {
|
|
|
|
closureCallFunc(closuresPtr, bean)
|
2017-01-03 16:20:28 +08:00
|
|
|
|
2020-03-22 23:12:55 +08:00
|
|
|
if processor, ok := interface{}(bean).(AfterUpdateProcessor); ok {
|
|
|
|
processor.AfterUpdate()
|
2017-01-03 16:20:28 +08:00
|
|
|
}
|
2020-03-22 23:12:55 +08:00
|
|
|
}
|
|
|
|
for bean, closuresPtr := range session.afterDeleteBeans {
|
|
|
|
closureCallFunc(closuresPtr, bean)
|
2017-01-03 16:20:28 +08:00
|
|
|
|
2020-03-22 23:12:55 +08:00
|
|
|
if processor, ok := interface{}(bean).(AfterDeleteProcessor); ok {
|
|
|
|
processor.AfterDelete()
|
2017-01-03 16:20:28 +08:00
|
|
|
}
|
2020-03-22 23:12:55 +08:00
|
|
|
}
|
|
|
|
cleanUpFunc := func(slices *map[interface{}]*[]func(interface{})) {
|
|
|
|
if len(*slices) > 0 {
|
|
|
|
*slices = make(map[interface{}]*[]func(interface{}), 0)
|
2017-01-03 16:20:28 +08:00
|
|
|
}
|
|
|
|
}
|
2020-03-22 23:12:55 +08:00
|
|
|
cleanUpFunc(&session.afterInsertBeans)
|
|
|
|
cleanUpFunc(&session.afterUpdateBeans)
|
|
|
|
cleanUpFunc(&session.afterDeleteBeans)
|
2017-01-03 16:20:28 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2021-05-15 03:17:06 +08:00
|
|
|
|
|
|
|
// IsInTx if current session is in a transaction
|
|
|
|
func (session *Session) IsInTx() bool {
|
|
|
|
return !session.isAutoCommit
|
|
|
|
}
|