Skip to content

Commit

Permalink
Exposing session to the public
Browse files Browse the repository at this point in the history
  • Loading branch information
uzarubin authored and moehlone committed Mar 31, 2017
1 parent a0d4948 commit 0be4b3f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 15 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@ _testmain.go
*.exe
*.test
*.prof

# Intellij
.idea
2 changes: 1 addition & 1 deletion document_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ func (self *DocumentBase) Save() error {
* original session to wait." see: http://godoc.org/labix.org/v2/mgo#Session.Clone
*/

session := self.connection.session.Clone()
session := self.connection.Session.Clone()
defer session.Close()

collection := session.DB(self.connection.Config.DatabaseName).C(self.collection.Name)
Expand Down
22 changes: 8 additions & 14 deletions mongodm.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ type (
//The "Database" object which stores all connections
Connection struct {
Config *Config
session *mgo.Session
database *mgo.Database
Session *mgo.Session
modelRegistry map[string]*Model
typeRegistry map[string]reflect.Type
}
Expand Down Expand Up @@ -161,8 +160,7 @@ func Connect(config *Config) (*Connection, error) {

con := &Connection{
Config: config,
session: nil,
database: nil,
Session: nil,
modelRegistry: make(map[string]*Model),
typeRegistry: make(map[string]reflect.Type),
}
Expand Down Expand Up @@ -248,7 +246,7 @@ func (self *Connection) Model(typeName string) *Model {
It is necessary to register your created models to the ODM to work with. Within this process
the ODM creates an internal model and type registry to work fully automatically and consistent.
Make sure you already created a connection. Registration expects a pointer to an IDocumentBase
type and the collection name where the docuements should be stored in.
type and the collection name where the documents should be stored in.
For example:
connection.Register(&User{}, "users")
Expand All @@ -266,8 +264,7 @@ func (self *Connection) Register(document IDocumentBase, collectionName string)

//check if model was already registered
if _, ok := self.modelRegistry[typeName]; !ok {

collection := self.database.C(collectionName)
collection := self.Session.DB("").C(collectionName) // empty string returns db name from dial info
model := &Model{collection, self}

self.modelRegistry[typeName] = model
Expand Down Expand Up @@ -311,19 +308,16 @@ func (self *Connection) Open() (err error) {
return err
}

self.session = session

self.session.SetMode(mgo.Monotonic, true)

self.database = self.session.DB(self.Config.DatabaseName)
self.Session = session

self.Session.SetMode(mgo.Monotonic, true)
return nil
}

//Closes an existing database connection
func (self *Connection) Close() {

if self.session != nil {
self.session.Close()
if self.Session != nil {
self.Session.Close()
}
}

0 comments on commit 0be4b3f

Please sign in to comment.