use predefined Succcess/Failure
This commit is contained in:
parent
a35d48cf0f
commit
a28f0f3430
@ -98,14 +98,14 @@ func ThroughBarrierCall(db *sql.DB, transInfo *TransInfo, busiCall BusiFunc) (re
|
|||||||
currentAffected, rerr := insertBarrier(tx, ti.TransType, ti.Gid, ti.BranchID, ti.BranchType, ti.BranchType)
|
currentAffected, rerr := insertBarrier(tx, ti.TransType, ti.Gid, ti.BranchID, ti.BranchType, ti.BranchType)
|
||||||
logrus.Printf("originAffected: %d currentAffected: %d", originAffected, currentAffected)
|
logrus.Printf("originAffected: %d currentAffected: %d", originAffected, currentAffected)
|
||||||
if (ti.BranchType == "cancel" || ti.BranchType == "compensate") && originAffected > 0 { // 这个是空补偿,返回成功
|
if (ti.BranchType == "cancel" || ti.BranchType == "compensate") && originAffected > 0 { // 这个是空补偿,返回成功
|
||||||
res = common.MS{"dtm_result": "SUCCESS"}
|
res = ResultSuccess
|
||||||
return
|
return
|
||||||
} else if currentAffected == 0 { // 插入不成功
|
} else if currentAffected == 0 { // 插入不成功
|
||||||
var result sql.NullString
|
var result sql.NullString
|
||||||
err := common.StxQueryRow(tx, "select result from dtm_barrier.barrier where trans_type=? and gid=? and branch_id=? and branch_type=? and reason=?",
|
err := common.StxQueryRow(tx, "select result from dtm_barrier.barrier where trans_type=? and gid=? and branch_id=? and branch_type=? and reason=?",
|
||||||
ti.TransType, ti.Gid, ti.BranchID, ti.BranchType, ti.BranchType).Scan(&result)
|
ti.TransType, ti.Gid, ti.BranchID, ti.BranchType, ti.BranchType).Scan(&result)
|
||||||
if err == sql.ErrNoRows { // 这个是悬挂操作,返回失败,AP收到这个返回,会尽快回滚
|
if err == sql.ErrNoRows { // 这个是悬挂操作,返回失败,AP收到这个返回,会尽快回滚
|
||||||
res = common.MS{"dtm_result": "FAILURE"}
|
res = ResultFailure
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -117,7 +117,7 @@ func ThroughBarrierCall(db *sql.DB, transInfo *TransInfo, busiCall BusiFunc) (re
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
// 数据库里没有上次的结果,属于重复空补偿,直接返回成功
|
// 数据库里没有上次的结果,属于重复空补偿,直接返回成功
|
||||||
res = common.MS{"dtm_result": "SUCCESS"}
|
res = ResultSuccess
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
res, rerr = busiCall(tx)
|
res, rerr = busiCall(tx)
|
||||||
|
|||||||
@ -6,6 +6,7 @@ import (
|
|||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/yedf/dtm/common"
|
"github.com/yedf/dtm/common"
|
||||||
|
"github.com/yedf/dtm/dtmcli"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
"gorm.io/gorm/clause"
|
"gorm.io/gorm/clause"
|
||||||
)
|
)
|
||||||
@ -28,7 +29,7 @@ func prepare(c *gin.Context) (interface{}, error) {
|
|||||||
t := TransFromContext(c)
|
t := TransFromContext(c)
|
||||||
t.Status = "prepared"
|
t.Status = "prepared"
|
||||||
t.saveNew(dbGet())
|
t.saveNew(dbGet())
|
||||||
return M{"dtm_result": "SUCCESS"}, nil
|
return dtmcli.ResultSuccess, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func submit(c *gin.Context) (interface{}, error) {
|
func submit(c *gin.Context) (interface{}, error) {
|
||||||
@ -41,7 +42,7 @@ func submit(c *gin.Context) (interface{}, error) {
|
|||||||
t.Status = "submitted"
|
t.Status = "submitted"
|
||||||
t.saveNew(db)
|
t.saveNew(db)
|
||||||
go t.Process(db)
|
go t.Process(db)
|
||||||
return M{"dtm_result": "SUCCESS"}, nil
|
return dtmcli.ResultSuccess, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func abort(c *gin.Context) (interface{}, error) {
|
func abort(c *gin.Context) (interface{}, error) {
|
||||||
@ -52,7 +53,7 @@ func abort(c *gin.Context) (interface{}, error) {
|
|||||||
return M{"dtm_result": "FAILURE", "message": fmt.Sprintf("trans type: %s current status %s, cannot abort", dbt.TransType, dbt.Status)}, nil
|
return M{"dtm_result": "FAILURE", "message": fmt.Sprintf("trans type: %s current status %s, cannot abort", dbt.TransType, dbt.Status)}, nil
|
||||||
}
|
}
|
||||||
go dbt.Process(db)
|
go dbt.Process(db)
|
||||||
return M{"dtm_result": "SUCCESS"}, nil
|
return dtmcli.ResultSuccess, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func registerXaBranch(c *gin.Context) (interface{}, error) {
|
func registerXaBranch(c *gin.Context) (interface{}, error) {
|
||||||
@ -73,7 +74,7 @@ func registerXaBranch(c *gin.Context) (interface{}, error) {
|
|||||||
e2p(err)
|
e2p(err)
|
||||||
global := TransGlobal{Gid: branch.Gid}
|
global := TransGlobal{Gid: branch.Gid}
|
||||||
global.touch(db, config.TransCronInterval)
|
global.touch(db, config.TransCronInterval)
|
||||||
return M{"dtm_result": "SUCCESS"}, nil
|
return dtmcli.ResultSuccess, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func registerTccBranch(c *gin.Context) (interface{}, error) {
|
func registerTccBranch(c *gin.Context) (interface{}, error) {
|
||||||
@ -104,7 +105,7 @@ func registerTccBranch(c *gin.Context) (interface{}, error) {
|
|||||||
e2p(err)
|
e2p(err)
|
||||||
global := TransGlobal{Gid: branch.Gid}
|
global := TransGlobal{Gid: branch.Gid}
|
||||||
global.touch(dbGet(), config.TransCronInterval)
|
global.touch(dbGet(), config.TransCronInterval)
|
||||||
return M{"dtm_result": "SUCCESS"}, nil
|
return dtmcli.ResultSuccess, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func query(c *gin.Context) (interface{}, error) {
|
func query(c *gin.Context) (interface{}, error) {
|
||||||
|
|||||||
@ -33,7 +33,7 @@ func SagaBarrierAddRoute(app *gin.Engine) {
|
|||||||
|
|
||||||
func sagaBarrierAdjustBalance(sdb *sql.Tx, uid int, amount int) (interface{}, error) {
|
func sagaBarrierAdjustBalance(sdb *sql.Tx, uid int, amount int) (interface{}, error) {
|
||||||
_, err := common.StxExec(sdb, "update dtm_busi.user_account set balance = balance + ? where user_id = ?", amount, uid)
|
_, err := common.StxExec(sdb, "update dtm_busi.user_account set balance = balance + ? where user_id = ?", amount, uid)
|
||||||
return common.MS{"dtm_result": "SUCCESS"}, err
|
return dtmcli.ResultSuccess, err
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -56,7 +56,7 @@ func adjustBalance(sdb *sql.Tx, uid int, amount int) (interface{}, error) {
|
|||||||
if err == nil && affected == 0 {
|
if err == nil && affected == 0 {
|
||||||
return nil, fmt.Errorf("update 0 rows")
|
return nil, fmt.Errorf("update 0 rows")
|
||||||
}
|
}
|
||||||
return common.MS{"dtm_result": "SUCCESS"}, err
|
return dtmcli.ResultSuccess, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// TCC下,转入
|
// TCC下,转入
|
||||||
|
|||||||
@ -30,7 +30,7 @@ func XaFireRequest() string {
|
|||||||
gid := dtmcli.MustGenGid(DtmServer)
|
gid := dtmcli.MustGenGid(DtmServer)
|
||||||
_, err := XaClient.XaGlobalTransaction(gid, func(xa *dtmcli.Xa) (*resty.Response, error) {
|
_, err := XaClient.XaGlobalTransaction(gid, func(xa *dtmcli.Xa) (*resty.Response, error) {
|
||||||
resp, err := xa.CallBranch(&TransReq{Amount: 30}, Busi+"/TransOutXa")
|
resp, err := xa.CallBranch(&TransReq{Amount: 30}, Busi+"/TransOutXa")
|
||||||
if dtmcli.IsFailure(resp, err) {
|
if err != nil {
|
||||||
return resp, err
|
return resp, err
|
||||||
}
|
}
|
||||||
return xa.CallBranch(&TransReq{Amount: 30}, Busi+"/TransInXa")
|
return xa.CallBranch(&TransReq{Amount: 30}, Busi+"/TransInXa")
|
||||||
@ -42,19 +42,19 @@ func XaFireRequest() string {
|
|||||||
func xaTransIn(c *gin.Context) (interface{}, error) {
|
func xaTransIn(c *gin.Context) (interface{}, error) {
|
||||||
return XaClient.XaLocalTransaction(c, func(db *sql.DB, xa *dtmcli.Xa) (interface{}, error) {
|
return XaClient.XaLocalTransaction(c, func(db *sql.DB, xa *dtmcli.Xa) (interface{}, error) {
|
||||||
if reqFrom(c).TransInResult == "FAILURE" {
|
if reqFrom(c).TransInResult == "FAILURE" {
|
||||||
return M{"dtm_result": "FAILURE"}, nil
|
return dtmcli.ResultFailure, nil
|
||||||
}
|
}
|
||||||
_, err := common.SdbExec(db, "update dtm_busi.user_account set balance=balance+? where user_id=?", reqFrom(c).Amount, 2)
|
_, err := common.SdbExec(db, "update dtm_busi.user_account set balance=balance+? where user_id=?", reqFrom(c).Amount, 2)
|
||||||
return M{"dtm_result": "SUCCESS"}, err
|
return dtmcli.ResultSuccess, err
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func xaTransOut(c *gin.Context) (interface{}, error) {
|
func xaTransOut(c *gin.Context) (interface{}, error) {
|
||||||
return XaClient.XaLocalTransaction(c, func(db *sql.DB, xa *dtmcli.Xa) (interface{}, error) {
|
return XaClient.XaLocalTransaction(c, func(db *sql.DB, xa *dtmcli.Xa) (interface{}, error) {
|
||||||
if reqFrom(c).TransOutResult == "FAILURE" {
|
if reqFrom(c).TransOutResult == "FAILURE" {
|
||||||
return M{"dtm_result": "FAILURE"}, nil
|
return dtmcli.ResultFailure, nil
|
||||||
}
|
}
|
||||||
_, err := common.SdbExec(db, "update dtm_busi.user_account set balance=balance-? where user_id=?", reqFrom(c).Amount, 1)
|
_, err := common.SdbExec(db, "update dtm_busi.user_account set balance=balance-? where user_id=?", reqFrom(c).Amount, 1)
|
||||||
return M{"dtm_result": "SUCCESS"}, err
|
return dtmcli.ResultSuccess, err
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user