diff --git a/session.go b/session.go index 8b42aea..3675db2 100644 --- a/session.go +++ b/session.go @@ -10,6 +10,23 @@ var ( ErrNotFound = errors.New("not found") ) +type Connection struct { + connection *mgo.Session +} + +func NewConnection(dial string) (*Connection, error) { + connection, err := mgo.Dial(dial) + if err != nil { + return nil, err + } + connection.SetMode(mgo.Monotonic, true) + return &Connection{connection: connection}, nil +} + +func (c *Connection) Close() { + c.connection.Close() +} + type Session struct { session *mgo.Session } @@ -18,17 +35,8 @@ func (s *Session) Close() { s.session.Close() } -func (s *Session) Copy() *Session { - return &Session{session: s.session.Copy()} -} - -func NewSession(dial string) (*Session, error) { - session, err := mgo.Dial(dial) - if err != nil { - return nil, err - } - session.SetMode(mgo.Monotonic, true) - return &Session{session: session}, nil +func (c *Connection) NewSession() (*Session, error) { + return &Session{session: c.connection.Copy()}, nil } type Database struct {