On Thu, May 31, 2012 at 12:50 AM, Michael Bayer <mike...@zzzcomputing.com> 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.
Problem is, I'm stuck with strongly-referencing sessions. The app comes from SA 0.3, and is heavily relying on the session as a kind of L1 cache - removing that assumption is a really huge task we haven't gotten to. We managed to upgrade it to SA 0.5, but we kept strongly-referencing sessions. > 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. I've been solving those problems by reattaching objects to the session. Only with caches I haven't been able to do that, since cached objects will be used by many threads at once, so no single session can own them. >> 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. Interesting, I hadn't seen that example. Bookmarked already :-) Thanks. -- 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.