Skip to content

Commit

Permalink
Merge pull request #122 from sunmi-OS/feature/bugfix
Browse files Browse the repository at this point in the history
optimize db initialization methods
  • Loading branch information
luduoxin authored Aug 16, 2023
2 parents 01d35e8 + 82d9ee1 commit a10d5f5
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 6 deletions.
1 change: 0 additions & 1 deletion db/mongodb/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ func NewDB(dbname string) {
if err != nil {
panic(err)
}
dbMap.Delete(dbname)
dbMap.Store(dbname, orm)
if oldConn != nil {
db, _ := oldConn.(*mongo.Database)
Expand Down
8 changes: 3 additions & 5 deletions db/orm/gorm.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ func NewOrUpdateDB(dbname string) error {
if _Gorm == nil {
_Gorm = &Client{defaultDbName: defaultName}
}
// second: load gorm client
// load gorm client
oldGorm, _ := _Gorm.gormMaps.Load(dbname)
// first: open new gorm client
// open new gorm client
err = utils.Retry(func() error {
orm, err = openORM(dbname)
if err != nil {
Expand All @@ -72,10 +72,8 @@ func NewOrUpdateDB(dbname string) error {
}
return err
}
// third: delete old gorm client and store the new gorm client
_Gorm.gormMaps.Delete(dbname)
_Gorm.gormMaps.Store(dbname, orm)
// fourth: if old client is not nil, delete and close connection
// if old client is not nil, delete and close connection
if oldGorm != nil {
db, _ := oldGorm.(*gorm.DB).DB()
if db != nil {
Expand Down

0 comments on commit a10d5f5

Please sign in to comment.