On Aug 28, 2012, at 11:49 AM, Owen Nelson wrote:

> 
> As a guess, I think this has to do with how scoped sessions work, but exactly 
> how they work is a bit of a mystery to me. Have you seen anything like this 
> behavior before?
> I'm not quite sure how to investigate further into this issue, so any 
> pointers would be appreciated.

this is how the Scoped Session works:

import threading

class JustLikeScopedSession(object):
    def __init__(self):
        self.sessions = threading.local()

    def __call__(self):
        "__call__ turns a JustLikeScopedSession object into a callable, that 
is, JustLikeScopedSession()()"

        # return the Session for this thread.  if not present, create it.
        if hasattr(self.sessions, "the_session"):
            return self.sessions.the_session
       else:
            self.sessions.the_session = Session()
            return self.sessions.the_session

    def remove(self):
        # remove the Session for this thread.
        self.sessions.the_session.close()
        del self.sessions.the_session

    def __getattr__(self, key):
        "proxy method/attribute calls to the current thread's Session"
        return getattr(self(), key)

that's it.   If you understand what a threading.local() is (and what the 
__call__() method does), you're golden.  If not, then don't use 
scoped_session(), do things explicitly so that they make sense.

as far as state transferring along on tests, if you want to have some database 
state that carries forth on many tests, then your "setup" for the suite as a 
whole would create a Session, persist all the data you want, then commit() it.  
   There's no issue using sqlite :memory: for tests assuming everything occurs 
in the same process/thread across all your tests.     I often recommend a 
pattern of enclosing the Session in a transaction so that the data can be 
rolled back when all tests are complete (see the example at 
http://docs.sqlalchemy.org/en/rel_0_7/orm/session.html#joining-a-session-into-an-external-transaction),
 but with sqlite :memory: this is not really critical.

I'm not sure if i have more insight into your specific issue at the moment 
without more specifics.

-- 
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.

Reply via email to