dream3323 2016-05-05 02:42
浏览 47
已采纳

有没有一种标准的方法可以使数据库会话在Golang中的所有软件包之间保持打开状态?

I've been playing around with go for a bit now and I love it, but it seems to have a few things it does differently from other languages. So I'm writing a web app that uses MongoDb with the mgo package. I'm wondering what the best practice is for keeping a database session open for use in other packages (my models).

Feel free to correct me on any wrong ideals I might have, I've only started using GO.

Heres what I'm thinking:

package main

import(
    ds "api-v2/datastore"
)

type Log struct {
    Name string
}

func main() {
    sesh := ds.Sesh

    err = &sesh.Insert(&Log{"Ale"})
}

And in my datastore package:

package datastore

import(
    "gopkg.in/mgo.v2"
)

var Sesh = newSession()

func newSession() **mgo.Session {
    session, err := mgo.Dial("localhost")
    if err != nil {
        panic(err)
    } 

    return &session
}

Thanks!

  • 写回答

1条回答 默认 最新

  • dongliang7545 2016-05-05 06:01
    关注

    According to mgo's documentation, https://godoc.org/gopkg.in/mgo.v2:

    Every session created must have its Close method called at the end of its life time, so its resources may be put back in the pool or collected, depending on the case.

    The Close() need to be called as long as the jobs are done. And the session will be returned to pool when the method is called.

    Also, looking at their source code(https://github.com/go-mgo/mgo/blob/v2-unstable/session.go#L161), there are some comments on the Dial method

    // This method is generally called just once for a given cluster.  Further
    // sessions to the same cluster are then established using the New or Copy
    // methods on the obtained session. This will make them share the underlying
    // cluster, and manage the pool of connections appropriately.
    //
    // Once the session is not useful anymore, Close must be called to release the
    // resources appropriately.
    

    Dial method only need once for a single cluster. Use New or Copy for subsequent sessions, else you won't be beneficial from the advantage of connection pool.

    --- Updated ---

    I was trying to create an example but I found that the link from @John S Perayil's comment has quite a similar idea.

    What I wanted to stress out is that the Dial method you should call only once during startup, it is a good idea to put it into an init method. And then you could create a method (eg, newSession) that returns session.Copy().

    So this is how you would call the method:

    func main() {
        session := ds.NewSession()
        defer session.Close()
    
        err = &session.Insert(&Log{"Ale"})
    }
    

    Somehow I noticed that you didn't invoke defer session.Close() in your code, defer will execute the code right before the ending of the execution for this method.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 虚心请教几个问题,小生先有礼了
  • ¥30 截图中的mathematics程序转换成matlab