Hi,

I use a cache on some pages of my TurboGears2 application,  and I
encounter a problem.  There is a query in those page method, for
example:

@beaker_cache(expire=30, type='memory', query_args=True)
@expose()
def foo():
    data = DBSession.query(Bar).all()
    return dict(data=data)

Here comes the problem, the cache save the result of query in memory,
and before the cache is expired, it just return it from memory.  Which
means, there is some access to the record object out side the session
scope.  Here is a simple illustration.

-- First request to /foo
Enter method foo
   DBSession.create()
      data = DBSession.query(Bar).all()
      return data
   DBSession.end()
Exit method foo

-- Second request to /foo
Enter method foo
   DBSession.create()
      #data is not expired, return it directly
      return data
   DBSession.end()
Exit method foo

As you see, the second request return a query result from previous
request.  It is out side the scope of DBSession.  Then I got some
error like this:

<class 'sqlalchemy.exc.ProgrammingError'>: (ProgrammingError) (2014,
"Commands out of sync; you can't run this command now") 'SELECT
tag.name AS tag_name \nFROM tag, site_tag \nWHERE %s =
site_tag.user_id AND tag.name = site_tag.tag_name' (6790L,)

There is some relation properties on my table,  so that when I am
accessing those relation property out side the scope, it just can't
work.  For example, I have some tags on the site

class Site(DeclarativeBase):
    __tablename__ = 'site'

    # sites associate with this tag
    tags = relation('Tag', secondary=site_tag_table, backref='sites')

class Tag(DeclarativeBase):
    __tablename__ = 'tag'

    # name of this tag
    name = Column(Unicode(32), primary_key=True)

    def __unicode__(self):
        return self.name

Sometimes I access site.tags out side the scope, it will raise the
exception.

Back to the topic, my question is:
How to load those property manually, so that we don't have to load it
lazily?
For example, I might can call something like:

DBSession.load(site.tags)

Which tells SQLAlchemy to load it manually.

Thanks.
Victor Lin.

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