xa changed
This commit is contained in:
parent
cca406dca8
commit
2a36d597d7
@ -40,7 +40,7 @@ func CheckUserResponse(resp *resty.Response, err error) error {
|
||||
if resp.IsError() {
|
||||
return errors.New(resp.String())
|
||||
} else if strings.Contains(resp.String(), "FAILURE") {
|
||||
return ErrUserFailure
|
||||
return ErrFailure
|
||||
}
|
||||
}
|
||||
return err
|
||||
@ -130,8 +130,8 @@ func callDtmSimple(dtm string, body interface{}, operation string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// ErrUserFailure 表示用户返回失败,要求回滚
|
||||
var ErrUserFailure = errors.New("user return FAILURE")
|
||||
// ErrFailure 表示返回失败,要求回滚
|
||||
var ErrFailure = errors.New("transaction FAILURE")
|
||||
|
||||
// ErrDtmFailure 表示用户返回失败,要求回滚
|
||||
var ErrDtmFailure = errors.New("dtm return FAILURE")
|
||||
// ResultSuccess 表示返回成功,可以进行下一步
|
||||
var ResultSuccess = common.M{"dtm_result": "SUCCESS"}
|
||||
|
||||
20
dtmcli/xa.go
20
dtmcli/xa.go
@ -66,7 +66,7 @@ func (xc *XaClient) HandleCallback(gid string, branchID string, action string) (
|
||||
defer db.Close()
|
||||
xaID := gid + "-" + branchID
|
||||
_, err := common.SdbExec(db, fmt.Sprintf("xa %s '%s'", action, xaID))
|
||||
return M{"dtm_result": "SUCCESS"}, err
|
||||
return ResultSuccess, err
|
||||
|
||||
}
|
||||
|
||||
@ -78,17 +78,13 @@ func (xc *XaClient) XaLocalTransaction(c *gin.Context, xaFunc XaLocalFunc) (ret
|
||||
db := common.SdbAlone(xc.Conf)
|
||||
defer func() { db.Close() }()
|
||||
defer func() {
|
||||
var x interface{}
|
||||
x := recover()
|
||||
_, err := common.SdbExec(db, fmt.Sprintf("XA end '%s'", xaBranch))
|
||||
if err != nil {
|
||||
common.RedLogf("sql db exec error: %v", err)
|
||||
}
|
||||
if x = recover(); x != nil || IsFailure(ret, rerr) {
|
||||
} else {
|
||||
if x == nil && rerr == nil && err == nil {
|
||||
_, err = common.SdbExec(db, fmt.Sprintf("XA prepare '%s'", xaBranch))
|
||||
}
|
||||
if err != nil {
|
||||
common.RedLogf("sql db exec error: %v", err)
|
||||
if rerr == nil {
|
||||
rerr = err
|
||||
}
|
||||
if x != nil {
|
||||
panic(x)
|
||||
@ -99,12 +95,10 @@ func (xc *XaClient) XaLocalTransaction(c *gin.Context, xaFunc XaLocalFunc) (ret
|
||||
return
|
||||
}
|
||||
ret, rerr = xaFunc(db, xa)
|
||||
if IsFailure(ret, rerr) {
|
||||
if rerr != nil {
|
||||
return
|
||||
}
|
||||
ret, rerr = common.RestyClient.R().
|
||||
SetBody(&M{"gid": xa.Gid, "branch_id": branchID, "trans_type": "xa", "status": "prepared", "url": xc.CallbackURL}).
|
||||
Post(xc.Server + "/registerXaBranch")
|
||||
_, rerr = callDtm(xc.Server, &M{"gid": xa.Gid, "branch_id": branchID, "trans_type": "xa", "status": "prepared", "url": xc.CallbackURL}, "registerXaBranch", &TransOptions{})
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@ -33,7 +33,7 @@ func xaNormal(t *testing.T) {
|
||||
_, err := xc.XaGlobalTransaction(gid, func(xa *dtmcli.Xa) (*resty.Response, error) {
|
||||
req := examples.GenTransReq(30, false, false)
|
||||
resp, err := xa.CallBranch(req, examples.Busi+"/TransOutXa")
|
||||
if dtmcli.IsFailure(resp, err) {
|
||||
if err != nil {
|
||||
return resp, err
|
||||
}
|
||||
return xa.CallBranch(req, examples.Busi+"/TransInXa")
|
||||
@ -49,13 +49,13 @@ func xaRollback(t *testing.T) {
|
||||
_, err := xc.XaGlobalTransaction(gid, func(xa *dtmcli.Xa) (*resty.Response, error) {
|
||||
req := &examples.TransReq{Amount: 30, TransInResult: "FAILURE"}
|
||||
resp, err := xa.CallBranch(req, examples.Busi+"/TransOutXa")
|
||||
if dtmcli.IsFailure(resp, err) {
|
||||
if err != nil {
|
||||
return resp, err
|
||||
}
|
||||
return xa.CallBranch(req, examples.Busi+"/TransInXa")
|
||||
})
|
||||
assert.Error(t, err)
|
||||
WaitTransProcessed(gid)
|
||||
assert.Equal(t, []string{"succeed", "prepared"}, getBranchesStatus(gid))
|
||||
assert.Equal(t, []string{"succeed", "prepared", "succeed", "prepared"}, getBranchesStatus(gid))
|
||||
assert.Equal(t, "failed", getTransStatus(gid))
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user