0

Here is a snippet of my DAO implementation:

type (
    User struct {
        Name string `json:"name" bson:"name"`
        ...
    }

    UserDAO struct {
        *mgo.Database
    }
)

func NewUserDAO() (*UserDAO, error) {
    session, err := mgo.Dial("mongodb://127.0.0.1:27017/test")
    if err != nil {
        return nil, err
    }

    return &UserDAO{session.DB("")}, nil
}

func (d *UserDAO) Insert(user User) error {
    return d.C("users").Insert(user)
}

func (d *CandleDAO) Find(name string) ([]User, error) {
    var result []User
    if err := d.C("users").Find(bson.M{"name": name)}).Sort("time").All(&result); err != nil {
        return nil, err
    }
    return result, nil
}

...

func (d *CandleDAO) Close() {
    d.Session.Close()
}

And here is how I invoke it:

dao, err := dao.NewUserDAO()
if err != nil {
    Log.Error(err.Error())
    return
}

// close session... is this OK?
defer dao.Close()

users, err := dao.Find(&broker.Symbol{"BTC", "USD"}); if err != nil {
    Log.Error(err.Error())
    return
}

for i, user := range users {
    fmt.Printf("%d ==> %v\n", i, user)
}

The code above works... and my question is simple: shall I keep a global mgo.Database instance or is it correct to always close the session when I'm finished and create a new one whenever I need?

j3d
  • 9,492
  • 22
  • 88
  • 172
  • Related / possible duplicate of [too many open files in mgo go server](https://stackoverflow.com/questions/47179890/too-many-open-files-in-mgo-go-server/47180097#47180097); and [Concurrency in gopkg.in/mgo.v2 (Mongo, Go)](https://stackoverflow.com/questions/42492020/concurrency-in-gopkg-in-mgo-v2-mongo-go/42495522#42495522); and [mgo - query performance seems consistently slow (500-650ms)](https://stackoverflow.com/questions/40999637/mgo-query-performance-seems-consistently-slow-500-650ms/41000876#41000876). – icza Nov 11 '17 at 18:07
  • I think that this can help https://stackoverflow.com/questions/18650890/keeping-open-a-mongodb-database-connection. In my case I'm using go and bigtable, and I am keeping the connection alive (unless/until the server crashes). – mayo Nov 11 '17 at 18:09
  • Ok thank you very much, really appreciated :-) – j3d Nov 11 '17 at 18:21
  • Possible duplicate of [Concurrency in gopkg.in/mgo.v2 (Mongo, Go)](https://stackoverflow.com/questions/42492020/concurrency-in-gopkg-in-mgo-v2-mongo-go) – Adrian Nov 13 '17 at 21:05

1 Answers1

1

Per the main package docs:

New sessions are typically created by calling session.Copy on the initial session obtained at dial time. These new sessions will share the same cluster information and connection pool, and may be easily handed into other methods and functions for organizing logic. 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.

Meaning: at startup you should Dial and save that session, and for each call to NewUserDAO you should Clone the initial Session created with Dial.

Adrian
  • 42,911
  • 6
  • 107
  • 99