I've been a fan of tacking the connection onto the request object using
@reify, thus if your request never calls "request.db" it doesn't have any db
overhead.

Thanks to reify, it reuses the same connection throughout that request. If
you want to ensure that the connection is closed at the end of the request,
you can also add a finished_callback to handle that.

class MyRequest(Request):
    @reify
    def db(self):
        # do stuff to get connection either from middleware in previous
email
        # or actually create a connection
        conn =

        # optional: add callback to cleanup after request is complete
        def _cleanup(request):
            conn.close()
        self.add_finished_callback(_cleanup)
        return conn

The cleanup and things is handled by repoze.tm2 if using something like
SQLAlchemy with the ZopeTransactionExtension, but otherwise you'll probably
want to handle it yourself.


Michael


On Sun, Mar 13, 2011 at 8:54 AM, Fernando Correa Neto <fcd...@gmail.com>wrote:

> Hi
>
> On Sun, Mar 13, 2011 at 5:54 AM, Seth <seedifferen...@gmail.com> wrote:
> [snip]
> > How are other people connecting to a db with high-traffic sites using
> > Pyramid and avoiding the static_route re-connection nonsense? Perhaps we
> > could request to get a matched_route object on the NewRequest event
> object?
>
> I usually try to stick to a connection that is always on environ.
> You'd have a wsgi middleware that always try to get the connection
> from the threadinglocal and expose that under some environ key, say,
> 'conn.mongo'.
> If the middleware fails to get that from the thread, you'd create
> another connection and then re-expose it under the same key.
>
> Regards,
> Fernando
>
> --
> You received this message because you are subscribed to the Google Groups
> "pylons-devel" group.
> To post to this group, send email to pylons-devel@googlegroups.com.
> To unsubscribe from this group, send email to
> pylons-devel+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/pylons-devel?hl=en.
>
>

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

Reply via email to