0

When my server starts up, it will create a connection to a mongo database, grab that *mgo.Session and store it in a "server" struct for handlers defined on that struct to use to serve requests.

I see two ways to actually execute this.

1) Save the `*mgo.Session` in the struct.

The upside of this is that you can call Session.Copy in each handler before using the session to have connection isolation. The downside is that you need to call .DB("x").C("y") for a specific x and y for each handler. If you wanted to change this, you need to find each instance where you're using it and change it. That's less than ideal.

2) Store the `*mgo.Database` or even `*mgo.Collection` object on the server struct.

The upside is that you can configure it in one place and use it. The downside is that there appears to be no way to use Copy() on the thing for connection isolation.

How do you recommend storing the mongo session so that you can cleanly use the connection and provide isolation between requests?

MichaelB
  • 1,092
  • 2
  • 16
  • 32

0 Answers0