3 Commits

Author SHA1 Message Date
f8f8cb2366 removed debug msg 2022-02-10 15:33:20 +01:00
6a7a6135da close finalizer 2022-02-10 15:00:33 +01:00
709ca7f23b close finalizer 2022-02-10 14:59:39 +01:00

27
mgo.go
View File

@@ -2,12 +2,14 @@ package mgocrud
import (
"errors"
"runtime"
mgo "gopkg.in/mgo.v2"
)
type MgoConnection struct {
connection *mgo.Session
closed bool
}
func NewMgoConnection(dial string) (Connection, error) {
@@ -16,23 +18,44 @@ func NewMgoConnection(dial string) (Connection, error) {
return nil, err
}
connection.SetMode(mgo.Monotonic, true)
return &MgoConnection{connection: connection}, nil
c := &MgoConnection{connection: connection}
runtime.SetFinalizer(c, func(c *MgoConnection) {
if !c.closed {
c.Close()
}
})
return c, nil
}
func (c *MgoConnection) Close() {
if !c.closed {
c.connection.Close()
c.closed = true
}
runtime.SetFinalizer(c, nil)
}
type MgoSession struct {
session *mgo.Session
closed bool
}
func (s *MgoSession) Close() {
if !s.closed {
s.session.Close()
s.closed = true
}
runtime.SetFinalizer(s, nil)
}
func (c *MgoConnection) NewSession() Session {
return &MgoSession{session: c.connection.Copy()}
s := &MgoSession{session: c.connection.Copy()}
runtime.SetFinalizer(s, func(s *MgoSession) {
if !s.closed {
s.Close()
}
})
return s
}
type MgoDatabase struct {