On May 30, 2012, at 8:53 PM, Claudio Freire wrote:

> 
> Thing is, in order to work with a large volume of objects, you're
> forced to do this, otherwise the session can grow uncontrollably.

flush periodically, and don't maintain references to things you're done with.  
The Session does not strongly reference objects that have no pending changes, 
and they'll be garbage collected.


> When
> you separate the operation to work in batches, you almost always have
> some objects that have a lifespan larger than a single batch, and then
> a single session.

Working in batches is fine.  You only need a single Session for all those 
batches, and a single transaction.    If you want several transactions, also 
fine, call commit() periodically.  In none of these cases does the Session need 
to be closed, and all objects worked with thus far which are still referenced 
in memory can remain attached to that Session, and you wont have any detachment 
errors.

The problems you're having are from unnecessary detachment of objects, from 
calling Session.close() and continuing to work with objects that have lost 
their owning Session, within the context of a new Session they have no 
association with.   

> 
> Another case in which an object's lifespan can exceed the session's,
> is when you want to implement caching with objects of your data model
> - cached values will have come from other sessions than the current
> one, and things get horribly messy.

There are documented patterns for caching - see the example in 
examples/beaker_caching in the distro.   This pattern is designed to cleanly 
handle the pattern of detached objects becoming re-associated with a particular 
session at once.   The pattern is along the lines of, session is created to 
work with a field of objects, a set of objects is retrieved from the cache, 
then re-associated with the cache en-masse using the merge_result() method 
illustrated in the example.



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