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,10 +401,12 @@ func (scope *Scope) InstanceGet(name string) (interface{}, bool) {
// Begin start a transaction // Begin start a transaction
func (scope *Scope) Begin() *Scope { func (scope *Scope) Begin() *Scope {
if db, ok := scope.SQLDB().(sqlDb); ok { if _, ok := scope.Get("xa"); !ok {
if tx, err := db.Begin(); scope.Err(err) == nil { if db, ok := scope.SQLDB().(sqlDb); ok {
scope.db.db = interface{}(tx).(SQLCommon) if tx, err := db.Begin(); scope.Err(err) == nil {
scope.InstanceSet("gorm:started_transaction", true) scope.db.db = interface{}(tx).(SQLCommon)
scope.InstanceSet("gorm:started_transaction", true)
}
} }
} }
return scope return scope
@ -412,14 +414,16 @@ func (scope *Scope) Begin() *Scope {
// CommitOrRollback commit current transaction if no error happened, otherwise will rollback it // CommitOrRollback commit current transaction if no error happened, otherwise will rollback it
func (scope *Scope) CommitOrRollback() *Scope { func (scope *Scope) CommitOrRollback() *Scope {
if _, ok := scope.InstanceGet("gorm:started_transaction"); ok { if _, ok := scope.Get("xa"); !ok {
if db, ok := scope.db.db.(sqlTx); ok { if _, ok := scope.InstanceGet("gorm:started_transaction"); ok {
if scope.HasError() { if db, ok := scope.db.db.(sqlTx); ok {
db.Rollback() if scope.HasError() {
} else { db.Rollback()
scope.Err(db.Commit()) } else {
scope.Err(db.Commit())
}
scope.db.db = scope.db.parent.db
} }
scope.db.db = scope.db.parent.db
} }
} }
return scope return scope
@ -857,8 +861,10 @@ func (scope *Scope) inlineCondition(values ...interface{}) *Scope {
func (scope *Scope) callCallbacks(funcs []*func(s *Scope)) *Scope { func (scope *Scope) callCallbacks(funcs []*func(s *Scope)) *Scope {
defer func() { defer func() {
if err := recover(); err != nil { if err := recover(); err != nil {
if db, ok := scope.db.db.(sqlTx); ok { if _, ok := scope.Get("xa"); !ok {
db.Rollback() if db, ok := scope.db.db.(sqlTx); ok {
db.Rollback()
}
} }
panic(err) panic(err)
} }