mirror of https://github.com/dtm-labs/dtm.git
19 changed files with 180 additions and 96 deletions
@ -0,0 +1,18 @@ |
|||||
|
package dtmcli |
||||
|
|
||||
|
import ( |
||||
|
"database/sql" |
||||
|
) |
||||
|
|
||||
|
// DB inteface of dtmcli db
|
||||
|
type DB interface { |
||||
|
Exec(query string, args ...interface{}) (sql.Result, error) |
||||
|
QueryRow(query string, args ...interface{}) *sql.Row |
||||
|
} |
||||
|
|
||||
|
// Tx interface of dtmcli tx
|
||||
|
type Tx interface { |
||||
|
Rollback() error |
||||
|
Commit() error |
||||
|
DB |
||||
|
} |
||||
@ -0,0 +1,22 @@ |
|||||
|
package examples |
||||
|
|
||||
|
import ( |
||||
|
"github.com/go-resty/resty/v2" |
||||
|
"github.com/yedf/dtm/dtmcli" |
||||
|
) |
||||
|
|
||||
|
func init() { |
||||
|
addSample("xa_gorm", func() string { |
||||
|
gid := dtmcli.MustGenGid(DtmServer) |
||||
|
err := XaClient.XaGlobalTransaction(gid, func(xa *dtmcli.Xa) (*resty.Response, error) { |
||||
|
resp, err := xa.CallBranch(&TransReq{Amount: 30}, Busi+"/TransOutXaGorm") |
||||
|
if err != nil { |
||||
|
return resp, err |
||||
|
} |
||||
|
return xa.CallBranch(&TransReq{Amount: 30}, Busi+"/TransInXa") |
||||
|
}) |
||||
|
dtmcli.FatalIfError(err) |
||||
|
return gid |
||||
|
}) |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,34 @@ |
|||||
|
package examples |
||||
|
|
||||
|
import ( |
||||
|
"github.com/gin-gonic/gin" |
||||
|
"github.com/yedf/dtm/common" |
||||
|
"github.com/yedf/dtm/dtmcli" |
||||
|
) |
||||
|
|
||||
|
func init() { |
||||
|
setupFuncs["SagaGormBarrierSetup"] = func(app *gin.Engine) { |
||||
|
app.POST(BusiAPI+"/SagaBTransOutGorm", common.WrapHandler(sagaGormBarrierTransOut)) |
||||
|
} |
||||
|
addSample("saga_gorm_barrier", func() string { |
||||
|
dtmcli.Logf("a busi transaction begin") |
||||
|
req := &TransReq{Amount: 30} |
||||
|
saga := dtmcli.NewSaga(DtmServer, dtmcli.MustGenGid(DtmServer)). |
||||
|
Add(Busi+"/SagaBTransOutGorm", Busi+"/SagaBTransOutCompensate", req). |
||||
|
Add(Busi+"/SagaBTransIn", Busi+"/SagaBTransInCompensate", req) |
||||
|
dtmcli.Logf("busi trans submit") |
||||
|
err := saga.Submit() |
||||
|
dtmcli.FatalIfError(err) |
||||
|
return saga.Gid |
||||
|
}) |
||||
|
|
||||
|
} |
||||
|
|
||||
|
func sagaGormBarrierTransOut(c *gin.Context) (interface{}, error) { |
||||
|
req := reqFrom(c) |
||||
|
barrier := MustBarrierFromGin(c) |
||||
|
tx := dbGet().DB.Begin() |
||||
|
return dtmcli.ResultSuccess, barrier.Call(tx.Statement.ConnPool.(dtmcli.Tx), func(db dtmcli.DB) error { |
||||
|
return tx.Exec("update dtm_busi.user_account set balance = balance + ? where user_id = ?", -req.Amount, 2).Error |
||||
|
}) |
||||
|
} |
||||
Loading…
Reference in new issue