Skip to content

Best practice #76

@pjebs

Description

@pjebs

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.

  1. Call Dial when we need to access database and close as soon as possible (usually with defer within same function).

  2. 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.
  1. 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?

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions