Hi, Im using mongoengine as mongodb connection wrapper too.

If you want to init connection for mongoengine, you can call connect()  
on your application main function.

In your application main::

    from mongoengine import connect as mongo_connect

    def main(global_config, **settings):
        # Register and connect databases.
        mongo_connect(
            settings['mongodb.database'],
            host=settings['mongodb.host'],
            port=int(settings['mongodb.port']))

         
… other initialization code …

I think it is best way to manage connection of databases.


--  
Takahiro Fujiwara
Twitter: @tfmagician
Mail: [email protected]


On Saturday, August 11, 2012 at 1:35 PM, Zak wrote:

> I'm receiving an error: global name Root is not defined
>  
> On Friday, March 25, 2011 5:49:31 PM UTC-5, oO wrote:
> > I'm not using MongoEngine, so I don't know about the particulars. I am 
> > however using PyMongo, and I'm using a different setup from the cookbook.
> >  
> > I setup the mongo connection as part of the RootFactory object (the one in 
> > config that returns the root resource)
> > When creating the root object, the root factory adds the already existing 
> > connection to the root resource root.db
> > views can always access the resource context, and the resource context can 
> > always access the root, and therefore the mongo db.
> >  
> > class MongoRootFactory(object):
> >     """Root Factory with a MongoDB connection"""
> >      
> >     def __init__(self, settings):
> >         """Initialize the factory by creating the mongo connection"""
> >         self.__mongo_uri = settings['mongo.uri']
> >         self.__mongo_db = settings['mongo.db']
> >         #Create the MongoDB connection     
> >         self.connection = pymongo.Connection(self.__mongo_uri)
> >         self.db = self.connection[ self.__mongo_db ]
> >         self.fs = GridFS(self.db)
> >         #Add the manipulator
> >         self.db.add_son_manipulator( AutoDateManipulator() )
> >      
> >     def __call__(self, request):
> >         """Return the root of the resource hierarchy"""
> >         #Create the root resource if it doesn't exists.
> >         if self.root == None:
> >             self.root = self.create_root()
> >         #Update the request object so the resource can access it.
> >         self.root.request = request
> >         self.root.db = self.db
> >         self.root.fs = self.fs
> >         return self.root
> >  
> >     def create_root(self):
> >         #log.debug("RootFactory: create the root object")
> >         return Root()
> >  
> >  
> > The RootFactory object lives inside the application, so the connection 
> > doesn't get created/closed for every request. I also find that the DB 
> > connection is something that should belong to the resources, not the 
> > request. (and you can always get it from the request using request.root.db
> >  
> > oO
> --  
> You received this message because you are subscribed to the Google Groups 
> "pylons-discuss" group.
> To view this discussion on the web visit 
> https://groups.google.com/d/msg/pylons-discuss/-/CX676Fb4qsAJ.
> To post to this group, send email to [email protected] 
> (mailto:[email protected]).
> To unsubscribe from this group, send email to 
> [email protected] 
> (mailto:[email protected]).
> For more options, visit this group at 
> http://groups.google.com/group/pylons-discuss?hl=en.

-- 
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss?hl=en.

Reply via email to