add xa support

This commit is contained in:
Edward.Yang 2019-11-16 00:10:41 +08:00
parent 179760d834
commit 5e1ba75d80

View File

@ -401,17 +401,20 @@ func (scope *Scope) InstanceGet(name string) (interface{}, bool) {
// Begin start a transaction
func (scope *Scope) Begin() *Scope {
if _, ok := scope.Get("xa"); !ok {
if db, ok := scope.SQLDB().(sqlDb); ok {
if tx, err := db.Begin(); scope.Err(err) == nil {
scope.db.db = interface{}(tx).(SQLCommon)
scope.InstanceSet("gorm:started_transaction", true)
}
}
}
return scope
}
// CommitOrRollback commit current transaction if no error happened, otherwise will rollback it
func (scope *Scope) CommitOrRollback() *Scope {
if _, ok := scope.Get("xa"); !ok {
if _, ok := scope.InstanceGet("gorm:started_transaction"); ok {
if db, ok := scope.db.db.(sqlTx); ok {
if scope.HasError() {
@ -422,6 +425,7 @@ func (scope *Scope) CommitOrRollback() *Scope {
scope.db.db = scope.db.parent.db
}
}
}
return scope
}
@ -857,9 +861,11 @@ func (scope *Scope) inlineCondition(values ...interface{}) *Scope {
func (scope *Scope) callCallbacks(funcs []*func(s *Scope)) *Scope {
defer func() {
if err := recover(); err != nil {
if _, ok := scope.Get("xa"); !ok {
if db, ok := scope.db.db.(sqlTx); ok {
db.Rollback()
}
}
panic(err)
}
}()