Commit 4c218b89 by zhengqiuyun86

初始化

parent cb5e7729
...@@ -6,8 +6,7 @@ type State int16 ...@@ -6,8 +6,7 @@ type State int16
const ( const (
BusinessError State = 1001 BusinessError State = 1001
OK State = 1000 OK State = 1000
Expire State = 1 - iota Illegal1 State = 1 - iota
Illegal1
Illegal2 Illegal2
Illegal3 Illegal3
Illegal4 Illegal4
...@@ -24,8 +23,6 @@ func (t State) String() string { ...@@ -24,8 +23,6 @@ func (t State) String() string {
return "business error" return "business error"
case OK: case OK:
return "OK" return "OK"
case Expire:
return "expire request"
case Illegal1: case Illegal1:
return "illegal request(1)" return "illegal request(1)"
case Illegal2: case Illegal2:
......
...@@ -22,6 +22,8 @@ func TestLog() { ...@@ -22,6 +22,8 @@ func TestLog() {
var customerDb *gorm.DB var customerDb *gorm.DB
var customerDbMap sync.Map
var gidNeedTX sync.Map var gidNeedTX sync.Map
func SetTx() { func SetTx() {
...@@ -30,18 +32,24 @@ func SetTx() { ...@@ -30,18 +32,24 @@ func SetTx() {
} }
func GetDb() *gorm.DB { 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() gid := util.GetGID()
_, needTx := gidNeedTX.Load(gid) _, needTx := gidNeedTX.Load(gid)
if needTx { if needTx {
cacheTx, ok := dbMap.Load(gid)
if ok {
return cacheTx.(*gorm.DB)
} else {
if conf.LogShowSql { if conf.LogShowSql {
db = db.Debug() db = db.Debug()
} }
tx := db.Begin() tx := db.Begin()
dbMap.Store(gid, tx)
return tx return tx
}
} else { } else {
if conf.LogShowSql { if conf.LogShowSql {
db = db.Debug() db = db.Debug()
...@@ -51,11 +59,41 @@ func getDb(db *gorm.DB) *gorm.DB { ...@@ -51,11 +59,41 @@ func getDb(db *gorm.DB) *gorm.DB {
} }
func Rollback() { func Rollback() {
customerDb.Rollback() gid := util.GetGID()
_, needTx := gidNeedTX.Load(gid)
if needTx {
rollbackTx(gid, &customerDbMap)
gidNeedTX.Delete(gid)
}
} }
func Commit() { 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() { 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