添加数据库放回方法

This commit is contained in:
zhucheer 2019-12-24 21:03:38 +08:00
parent f7611e3679
commit 0817ed8a11
3 changed files with 17 additions and 4 deletions

View File

@ -10,7 +10,7 @@ type DataBase interface {
RegisterAll()
Register(string)
insertPool(string, pool.Pool)
getDB(string) (interface{},func(), error)
getDB(string) (interface{}, func(), error)
putDB(string, interface{}) error
}
@ -20,6 +20,15 @@ type dbChan struct {
conn interface{}
}
// PutConn 将连接放回连接池方法
func PutConn(put func()) {
if put == nil {
return
}
put()
return
}
// PullChanDB 持续监听将chan中的连接异步放回连接池
func PullChanDB() {
if mysqlConn.count+redisConn.count == 0 {

View File

@ -27,7 +27,7 @@
addr = "192.168.137.100:3306"
username = "zhuqi"
password = "123456"
dbname = "douya"
dbname = "weixin"
[database.redis]
[database.redis.default]
addr = "192.168.137.100:6379"

View File

@ -67,14 +67,18 @@ type WxUsers struct {
} // 默认表名是`users`
func SelectMySql(c *app.Context) error {
db, put, err := database.GetMysql("default")
defer put()
defer func() {
fmt.Println("put conn====>")
database.PutConn(put)
}()
if err != nil {
fmt.Println("db connect error", err)
return nil
}
info := &WxUsers{}
db.Table("wx_users").Where("id = ?", "1").First(&info)
db.Table("qi_user").Where("id > ?", "1").First(&info)
fmt.Println("db find====")
return c.ToJson(map[string]interface{}{