After some tests with weak_identity_map=False see that the source of the ORM queries for attributes is not related to garbage collection. They aren't issued if query caching is off, so the problem is related to caching.
When I write: theme = session.query(Theme).cache_key(....).get(..) # gets from cache print theme.id # here the 'id' attribute access issues a new SELECT When the cached object is accessed first, no queries are issued, then I do a lot of other record creation and flushing (I'm assigning this 'theme' to that records' attributes), then the cache is accessed again and this is the moment when the unwanted queries are issued. It seems that my record creation and flushing between cache calls makes that the ORM expires the cached object. However, the 'theme' object is itself never modified. What might be important, they queries aren't issued if the whole long- running operation is run inside a big transaction. My caching query implementation is here: http://pastebin.com/mf738cc5 I am using a Session with autoflush=False, autocommit=True. Do you know what might cause that behavior? Thanks, Adam On 8 Lis, 00:03, Adam Dziendziel <[email protected]> wrote: > Hi, > > I am trying to use the query caching solution described > here:http://svn.sqlalchemy.org/sqlalchemy/trunk/examples/query_caching/per... > > In most cases it works, the returned records are cached, I store them > in a LRU cache modeled afterhttp://code.activestate.com/recipes/498245/ > > However, when I run a long running operation, which operates on > hundreds of other records, apparently the garbage collection is run on > the session's weak-referencing identity map. The cache keeps the > returned records, but other eagerly loaded related instances of the > returned records are lost. The ORM issues queries to load them again > from the database. I understand that there are no strong references > between an instance and other related instances. > > What is the best solution to keep related instances in a session? > > If I create a session with weak_identity_map=False, then during my > long running operation I will run out of memory, unless I expunge > unused records, however, it is easy to miss one record and the > identity map will be growing anyway. > > Is there possible to get a list of referenced instances of another > instance, so that I could store the list together with the instance in > the MRU cache? Or to make a session with a strong-referencing map and > LRU policy that keeps it below a given size? > > Regards, > Adam --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
