Commit 4c218b89 by zhengqiuyun86

初始化

parent cb5e7729
......@@ -6,8 +6,7 @@ type State int16
const (
BusinessError State = 1001
OK State = 1000
Expire State = 1 - iota
Illegal1
Illegal1 State = 1 - iota
Illegal2
Illegal3
Illegal4
......@@ -24,8 +23,6 @@ func (t State) String() string {
return "business error"
case OK:
return "OK"
case Expire:
return "expire request"
case Illegal1:
return "illegal request(1)"
case Illegal2:
......
......@@ -22,6 +22,8 @@ func TestLog() {
var customerDb *gorm.DB
var customerDbMap sync.Map
var gidNeedTX sync.Map
func SetTx() {
......@@ -30,18 +32,24 @@ func SetTx() {
}
func GetDb() *gorm.DB {
return getDb(customerDb)
return getDb(customerDb, &customerDbMap)
}
func getDb(db *gorm.DB) *gorm.DB {
func getDb(db *gorm.DB, dbMap *sync.Map) *gorm.DB {
gid := util.GetGID()
_, needTx := gidNeedTX.Load(gid)
if needTx {
cacheTx, ok := dbMap.Load(gid)
if ok {
return cacheTx.(*gorm.DB)
} else {
if conf.LogShowSql {
db = db.Debug()
}
tx := db.Begin()
dbMap.Store(gid, tx)
return tx
}
} else {
if conf.LogShowSql {
db = db.Debug()
......@@ -51,11 +59,41 @@ func getDb(db *gorm.DB) *gorm.DB {
}
func Rollback() {
customerDb.Rollback()
gid := util.GetGID()
_, needTx := gidNeedTX.Load(gid)
if needTx {
rollbackTx(gid, &customerDbMap)
gidNeedTX.Delete(gid)
}
}
func Commit() {
customerDb.Commit()
gid := util.GetGID()
_, needTx := gidNeedTX.Load(gid)
if needTx {
commitTx(gid, &customerDbMap)
gidNeedTX.Delete(gid)
}
}
func rollbackTx(gid uint64, dbMap *sync.Map) {
cacheTx, ok := dbMap.Load(gid)
if ok {
tx := cacheTx.(*gorm.DB)
tx.Rollback()
dbMap.Delete(gid)
}
}
func commitTx(gid uint64, dbMap *sync.Map) {
cacheTx, ok := dbMap.Load(gid)
if ok {
tx := cacheTx.(*gorm.DB)
tx.Commit()
dbMap.Delete(gid)
}
}
func Init() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment