- make gorm.DB singleton.
This commit is contained in:
@@ -9,7 +9,8 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
dbStr string
|
||||
dbStr string
|
||||
singletonDB *gorm.DB
|
||||
)
|
||||
|
||||
func Init() {
|
||||
@@ -17,12 +18,17 @@ func Init() {
|
||||
AutoMigrate()
|
||||
}
|
||||
|
||||
// todo gorm要求用单一的db?如果每次重新调用Open,会导致too many connection错误
|
||||
func GetDB() *gorm.DB {
|
||||
db, err := gorm.Open("mysql", dbStr)
|
||||
if err == nil {
|
||||
return db
|
||||
if singletonDB == nil {
|
||||
db, err := gorm.Open("mysql", dbStr)
|
||||
if err == nil {
|
||||
singletonDB = db
|
||||
return db
|
||||
}
|
||||
panic(fmt.Sprintf("AutoMigrate failed with error:%v", err))
|
||||
}
|
||||
panic(fmt.Sprintf("AutoMigrate failed with error:%v", err))
|
||||
return singletonDB
|
||||
}
|
||||
|
||||
func AutoMigrate() {
|
||||
@@ -38,4 +44,5 @@ func AutoMigrate() {
|
||||
db.AutoMigrate(&model.SkuVendorCategory{}, &model.StoreSkuCategoryMap{}, &model.SkuName{}, &model.Sku{}, &model.SkuNamePlaceBind{}, &model.StoreSkuBind{})
|
||||
db.Set("gorm:table_options", "CHARSET=utf8mb4").AutoMigrate(&model.SkuCategory{})
|
||||
db.AutoMigrate(&model.WeiXins{}, &model.JxBackendUser{})
|
||||
// db.AutoMigrate(&model.DurableTask{}, &model.DurableTaskItem{})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user