doufen1890 2016-12-21 08:01
浏览 192
已采纳

如何在GoLang中创建单例DB类

EDITED Solved:How should i create singleton DBManager class in GoLang.

I Referred few code sample of how to create go singleton but i wish to have methods in those and call them on their singleton reference. My Code is as follows

package dbprovider

import (
    "github.com/jinzhu/gorm"
    _"github.com/jinzhu/gorm/dialects/sqlite"
    "rest/article"
    "log"
)

type DBOperations interface {
    AddArticle(article *article.Article)
}

type DBManager struct {
    db            *gorm.DB
    isInitialized bool
}

var dbManagerInstance = new()

func GetDBManager() DBManager {
    return dbManagerInstance
}

func new() DBManager {
    localDbRef, err := gorm.Open("sqlite3", "../articles.db")
    if (err != nil) {
        panic("Error initializing db")
    } else {
        log.Print("DB Initialized successfully")
    }
    return DBManager{db:localDbRef, isInitialized:true}
}

func (dbManager DBManager)  AddArticle(article article.Article) (err error) {
    if (dbManager.isInitialized) {
        tx := dbManager.db.Begin()
        //dbManager.db.NewRecord(article)
        //dbManager.db.Commit()
        tx.NewRecord(article)
        tx.Commit()
        errs := dbManager.db.GetErrors()
        if (len(errs) > 0) {
            err = errs[0]
        } else {
            log.Print("No error in this transactions")
        }

    }
    return
}

With new answer i have updated this question including answer. But i have few queries. How to cathc and return exception from gorm.Create(..)

  • 写回答

1条回答 默认 最新

  • dongyidao1461 2016-12-21 08:17
    关注

    One way is to create an exported interface with the methods, and make the implementing type unexported. Create a global variable of the interface type, and initialize it with a package init() function. You don't need any synchronization as the package init() function will run only once, safely.

    Package init() functions are executed once, automatically, by the runtime, before you could refer to anything from the package. For details, see Spec: Package initialization.

    For example:

    package dbprovider
    
    type Manager interface {
        AddArticle(article *article.Article) error
        // Add other methods
    }
    
    type manager struct {
        db *gorm.DB
    }
    
    var Mgr Manager
    
    func init() {
        db, err := gorm.Open("sqlite3", "../articles.db")
        if err != nil {
            log.Fatal("Failed to init db:", err)
        }
        Mgr = &manager{db: db}
    }
    
    func (mgr *manager) AddArticle(article *article.Article) (err error) {
        mgr.db.Create(article)
        if errs := mgr.db.GetErrors(); len(errs) > 0 {
            err = errs[0]
        }
        return
    }
    

    Using it:

    import "dbprovider"
    
    if err := dbprovider.Mgr.AddArticle(someArticle); err != nil {
        // Handle error
    }
    

    You could also do it without an init() function, e.g.:

    var Mgr = newManager()
    
    func newManager() Manager {
        db, err := gorm.Open("sqlite3", "../articles.db")
        if err != nil {
            log.Fatal("Failed to init db:", err)
        }
        return &manager{db: db}
    }
    

    With this you may decide to make newManager() exported and users of your package could decide to use the shared Mgr instance, or they could create another Manager, e.g. for testing purposes.

    Notes: Mgr is an exported global variable, and it is possible to assign a new value to it by other packages (e.g. dbprovider.Mgr = nil). If you want to avoid this, you have to make it unexported, and provide a "getter" function for it, e.g.:

    var mgr = newManager()
    
    func Mgr() Manager { return mgr }
    

    And using it:

    err := dbprovider.Mgr().AddArticle(someArticle)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?