forked from domodwyer/mgo
-
Notifications
You must be signed in to change notification settings - Fork 228
Closed
Labels
Description
I notice there is a session, err := mgo.Dial(MONGODB_DIAL_URL) function and also Clone and Copy.
If we are using MongoDB for a web app backend, I'm wondering what the best practice is out of these options.
-
Call Dial when we need to access database and close as soon as possible (usually with defer within same function).
-
Call Dial at start of request and close at end of request cycle after servicing request.
- Obviously if we must do multiple queries per request, we use the same reference to session during
that request.
- Call Dial external to request cycle (i.e. when app starts up), store the reference in a global variable and keep using it independent of request and usually never close connection and just let the library do connection pooling.
(Obviously For option 2, an improvement is to dial lazily but that's obvious to readers).
Assuming that we may need to do multiple database queries per request, how exactly do we use clone and/or copy?