You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
84 lines
2.3 KiB
84 lines
2.3 KiB
8 years ago
|
// 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 {
|
||
7 years ago
|
if session.isAutoCommit {
|
||
6 years ago
|
tx, err := session.DB().BeginTx(session.ctx, nil)
|
||
8 years ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
7 years ago
|
session.isAutoCommit = false
|
||
|
session.isCommitedOrRollbacked = false
|
||
|
session.tx = tx
|
||
8 years ago
|
session.saveLastSQL("BEGIN TRANSACTION")
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
// Rollback When using transaction, you can rollback if any error
|
||
|
func (session *Session) Rollback() error {
|
||
7 years ago
|
if !session.isAutoCommit && !session.isCommitedOrRollbacked {
|
||
|
session.saveLastSQL(session.engine.dialect.RollBackStr())
|
||
|
session.isCommitedOrRollbacked = true
|
||
6 years ago
|
session.isAutoCommit = true
|
||
7 years ago
|
return session.tx.Rollback()
|
||
8 years ago
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
// Commit When using transaction, Commit will commit all operations.
|
||
|
func (session *Session) Commit() error {
|
||
7 years ago
|
if !session.isAutoCommit && !session.isCommitedOrRollbacked {
|
||
8 years ago
|
session.saveLastSQL("COMMIT")
|
||
7 years ago
|
session.isCommitedOrRollbacked = true
|
||
6 years ago
|
session.isAutoCommit = true
|
||
8 years ago
|
var err error
|
||
7 years ago
|
if err = session.tx.Commit(); err == nil {
|
||
8 years ago
|
// handle processors after tx committed
|
||
|
closureCallFunc := func(closuresPtr *[]func(interface{}), bean interface{}) {
|
||
|
if closuresPtr != nil {
|
||
|
for _, closure := range *closuresPtr {
|
||
|
closure(bean)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
for bean, closuresPtr := range session.afterInsertBeans {
|
||
|
closureCallFunc(closuresPtr, bean)
|
||
|
|
||
|
if processor, ok := interface{}(bean).(AfterInsertProcessor); ok {
|
||
|
processor.AfterInsert()
|
||
|
}
|
||
|
}
|
||
|
for bean, closuresPtr := range session.afterUpdateBeans {
|
||
|
closureCallFunc(closuresPtr, bean)
|
||
|
|
||
|
if processor, ok := interface{}(bean).(AfterUpdateProcessor); ok {
|
||
|
processor.AfterUpdate()
|
||
|
}
|
||
|
}
|
||
|
for bean, closuresPtr := range session.afterDeleteBeans {
|
||
|
closureCallFunc(closuresPtr, bean)
|
||
|
|
||
|
if processor, ok := interface{}(bean).(AfterDeleteProcessor); ok {
|
||
|
processor.AfterDelete()
|
||
|
}
|
||
|
}
|
||
|
cleanUpFunc := func(slices *map[interface{}]*[]func(interface{})) {
|
||
|
if len(*slices) > 0 {
|
||
|
*slices = make(map[interface{}]*[]func(interface{}), 0)
|
||
|
}
|
||
|
}
|
||
|
cleanUpFunc(&session.afterInsertBeans)
|
||
|
cleanUpFunc(&session.afterUpdateBeans)
|
||
|
cleanUpFunc(&session.afterDeleteBeans)
|
||
|
}
|
||
|
return err
|
||
|
}
|
||
|
return nil
|
||
|
}
|