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.