> -----Original Message----- > From: sqlalchemy@googlegroups.com [mailto:sqlalchemy@googlegroups.com] > On Behalf Of robert rottermann > Sent: 14 June 2011 10:53 > To: sqlalchemy@googlegroups.com > Subject: [sqlalchemy] question re using the session object > > hi there, > > for a zope website I am using sqlalchemy. > Now I am unsure how to use the session object. > > What I do now is: > from sqlalchemy.orm import scoped_session > ... > Session = scoped_session(session_factory, scopefunc) > session = Session() > > this session object I import into all classes where ever I need it. > > Now my question: > > is it ok to use this single instance troughout the life of the Zope > severer, or > should I call Session() whenever I need a session? > > thanks > robert >
You definitely shouldn't use your 'session' instance throughout the application - it won't be thread-safe. Scoped sessions are described at http://www.sqlalchemy.org/docs/orm/session.html#unitofwork-contextual, but basically, you have two choices. You can: a) Call Session() whenever you need a session. SA will ensure that if you call it twice within the same "scope" (which is typically the current thread), the same instance will be returned. b) Use your "Session" directly - it implements the same interface as the real session, and forwards all requests on to the underlying thread-local session. Hope that helps, Simon -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to sqlalchemy@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.