mirror of https://github.com/dtm-labs/dtm.git
csharpjavadistributed-transactionsdtmgogolangmicroservicenodejsphpdatabasesagaseatatcctransactiontransactionsxapythondistributed
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
55 lines
1.4 KiB
55 lines
1.4 KiB
package dtmsvr
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/yedf/dtm/common"
|
|
"gorm.io/gorm/clause"
|
|
)
|
|
|
|
func AddRoute(engine *gin.Engine) {
|
|
engine.POST("/api/dtmsvr/prepare", common.WrapHandler(Prepare))
|
|
engine.POST("/api/dtmsvr/commit", common.WrapHandler(Commit))
|
|
engine.POST("/api/dtmsvr/branch", common.WrapHandler(Branch))
|
|
engine.POST("/api/dtmsvr/rollback", common.WrapHandler(Rollback))
|
|
}
|
|
|
|
func Prepare(c *gin.Context) (interface{}, error) {
|
|
m := TransFromContext(c)
|
|
m.Status = "prepared"
|
|
m.SaveNew(dbGet())
|
|
return M{"message": "SUCCESS"}, nil
|
|
}
|
|
|
|
func Commit(c *gin.Context) (interface{}, error) {
|
|
db := dbGet()
|
|
m := TransFromContext(c)
|
|
m.Status = "committed"
|
|
m.SaveNew(db)
|
|
go m.Process(db)
|
|
return M{"message": "SUCCESS"}, nil
|
|
}
|
|
|
|
func Rollback(c *gin.Context) (interface{}, error) {
|
|
db := dbGet()
|
|
m := TransFromContext(c)
|
|
m = TransFromDb(db, m.Gid)
|
|
if m.TransType != "xa" || m.Status != "prepared" {
|
|
return nil, fmt.Errorf("unkown trans data. type: %s status: %s for gid: %s", m.TransType, m.Status, m.Gid)
|
|
}
|
|
// 当前xa trans的状态为prepared,直接处理,则是回滚
|
|
go m.Process(db)
|
|
return M{"message": "SUCCESS"}, nil
|
|
}
|
|
|
|
func Branch(c *gin.Context) (interface{}, error) {
|
|
branch := TransBranch{}
|
|
err := c.BindJSON(&branch)
|
|
e2p(err)
|
|
db := dbGet()
|
|
db.Must().Clauses(clause.OnConflict{
|
|
DoNothing: true,
|
|
}).Create(&branch)
|
|
return M{"message": "SUCCESS"}, nil
|
|
}
|
|
|