57 lines
1.8 KiB
Go
57 lines
1.8 KiB
Go
package examples
|
||
|
||
import (
|
||
"database/sql"
|
||
|
||
"github.com/gin-gonic/gin"
|
||
"github.com/yedf/dtm/common"
|
||
"github.com/yedf/dtm/dtmcli"
|
||
)
|
||
|
||
// XaClient XA client connection
|
||
var XaClient *dtmcli.XaClient = nil
|
||
|
||
// XaSetup 挂载http的api,创建XaClient
|
||
func XaSetup(app *gin.Engine) {
|
||
app.POST(BusiAPI+"/TransInXa", common.WrapHandler(xaTransIn))
|
||
app.POST(BusiAPI+"/TransOutXa", common.WrapHandler(xaTransOut))
|
||
var err error
|
||
XaClient, err = dtmcli.NewXaClient(DtmServer, config.DB, app, Busi+"/xa")
|
||
e2p(err)
|
||
}
|
||
|
||
// XaFireRequest 注册全局XA事务,调用XA的分支
|
||
func XaFireRequest() string {
|
||
gid := dtmcli.MustGenGid(DtmServer)
|
||
res, err := XaClient.XaGlobalTransaction(gid, func(xa *dtmcli.Xa) (interface{}, error) {
|
||
req := &TransReq{Amount: 30}
|
||
resp, err := xa.CallBranch(req, Busi+"/TransOutXa")
|
||
if dtmcli.IsFailure(resp, err) {
|
||
return resp, err
|
||
}
|
||
return xa.CallBranch(req, Busi+"/TransInXa")
|
||
})
|
||
dtmcli.PanicIfFailure(res, err)
|
||
return gid
|
||
}
|
||
|
||
func xaTransIn(c *gin.Context) (interface{}, error) {
|
||
return XaClient.XaLocalTransaction(c, func(db *sql.DB, xa *dtmcli.Xa) (interface{}, error) {
|
||
if reqFrom(c).TransInResult == "FAILURE" {
|
||
return M{"dtm_result": "FAILURE"}, nil
|
||
}
|
||
_, 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
|
||
})
|
||
}
|
||
|
||
func xaTransOut(c *gin.Context) (interface{}, error) {
|
||
return XaClient.XaLocalTransaction(c, func(db *sql.DB, xa *dtmcli.Xa) (interface{}, error) {
|
||
if reqFrom(c).TransOutResult == "FAILURE" {
|
||
return M{"dtm_result": "FAILURE"}, nil
|
||
}
|
||
_, 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
|
||
})
|
||
}
|