err, no, your test is incorrect.  You are maintaining a reference to the 
SessionTransaction in "tolist".

Change the middle of the loop to read:

   assert len(tolist) == 1
   del tolist

so that you are not artificially holding onto the SessionTransaction, and 
additionally:

   for s in sessions:
       #s.commit() # Not needed, and makes no difference
       s.remove()
      
   gc.collect()

before your last fetch of "tolist", otherwise uncollected cycles remain.   The 
fact that they go away, however, means that no references remain.  




On Jun 15, 2010, at 4:51 AM, Vinay Sajip wrote:

> import gc
> from sqlalchemy import create_engine
> from sqlalchemy.orm import sessionmaker, scoped_session
> from sqlalchemy.orm.session import SessionTransaction
> from sqlalchemy.pool import NullPool
> 
> engine = create_engine('sqlite://', poolclass=NullPool)
> sessions = []
> 
> # To simulate multi-db setup, create two sessions
> for i in range(2):
>    sessions.append(scoped_session(sessionmaker(bind=engine)))
> 
> for i in range(1, 3):
>    print 'Looping: iteration (request) #%d' % i
>    tolist = [c for c in gc.get_objects()
>              if isinstance(c, SessionTransaction)]
>    assert len(tolist) == 0
>    # Apparently scoped_sessions can be used without
>    # instantiating, so do that
>    # Just one of the databases will be used in the
>    # processing
>    s = sessions[0]
>    s.execute('select 1')
>    tolist = [c for c in gc.get_objects()
>              if isinstance(c, SessionTransaction)]
>    assert len(tolist) == 1
>    for s in sessions:
>        #s.commit() # Not needed, and makes no difference
>        s.remove()
>    # There should be no SessionTransactions remaining
>    tolist = [c for c in gc.get_objects()
>              if isinstance(c, SessionTransaction)]
>    # On my system, the next line raises AssertionError
>    assert len(tolist) == 0, 'No SessionTransactions hanging around'

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" 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/sqlalchemy?hl=en.

Reply via email to