forked from ocean-dev/go-lottery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysql_helper.go
56 lines (43 loc) · 962 Bytes
/
mysql_helper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package dataSource
import (
"fmt"
"log"
"sync"
_ "github.com/go-sql-driver/mysql"
"github.com/go-xorm/xorm"
"go-lottery/conf"
)
var MysqlMasterInst *xorm.Engine
var mysqlLock sync.Mutex
func MysqlInstMaster() *xorm.Engine {
if MysqlMasterInst != nil {
return MysqlMasterInst
}
// 处理高并发时避免重复定义实例
mysqlLock.Lock()
defer mysqlLock.Unlock()
if MysqlMasterInst != nil {
return MysqlMasterInst
}
return NewMysqlMaster()
}
func NewMysqlMaster() *xorm.Engine {
engine, err := xorm.NewEngine(
conf.MysqlDriverName,
fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8",
conf.MysqlMaster.User,
conf.MysqlMaster.Pwd,
conf.MysqlMaster.Host,
conf.MysqlMaster.Port,
conf.MysqlMaster.Database,
),
)
if err != nil {
log.Fatal("db_helper.NewMysqlMaster NewEngine error ", err)
return nil
}
// 本地调试打开 SQL 调试
engine.ShowSQL(true)
MysqlMasterInst = engine
return MysqlMasterInst
}