Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid caching in sqlalchemy?

I have a problem with SQL Alchemy - my app works as a constantly working python application.

I have function like this:

def myFunction(self, param1):
   s = select([statsModel.c.STA_ID, statsModel.c.STA_DATE)])\
                        .select_from(statsModel)

   statsResult = self.connection.execute(s).fetchall()

   return {'result': statsResult, 'calculation': param1}

I think this is clear example - one result set is fetched from database, second is just passed as argument.

The problem is that when I change data in my database, this function still returns data like nothing was changed. When I change data in input parameter, returned parameter "calculation" has proper value.

When I restart the app server, situation comes back to normal - new data are fetched from MySQL.

I know that there were several questions about SQLAlchemy caching like:

How to disable caching correctly in Sqlalchemy orm session?

How to disable SQLAlchemy caching?

but how other can I call this situation? It seems SQLAlchemy keeps the data fetched before and does not perform new queries until application restart. How can I avoid such behavior?

like image 525
Archarius Avatar asked Aug 24 '12 11:08

Archarius


People also ask

How does SQLAlchemy cache data?

SQLAlchemy supports two types of caches: 1 Caching the result set so that repeatedly running the same query hits the cache instead of the database. It uses dogpile... 2 Caching the query object so that Python interpreter doesn't have to manually re-assemble the query string every time. More ...

How does SQLAlchemy handle non-modifying queries?

This version works by simply committing the opened transaction as soon as SQLAlchemy detects an SQL statement that modifies data. Unfortunately, that doesn't fix our problem; the pointless, underlying DB transaction opened by non-modifying queries still remains open.

Is SQLAlchemy hurting your performance?

As you work with SQLAlchemy, over time, you might have a performance nightmare brewing in the background that you aren’t even aware of. In this lesser-known issue, which strikes primarily in larger projects, normal usage leads to an ever-growing number of idle-in-transaction database connections.

What are baked queries in SQLAlchemy?

These queries are called baked queries and the cache is called baked. Basically it caches all the actions sqlalchemy takes BEFORE hitting the database--it does not cut down on database calls. Initial benchmarks show speedups of up to 40% in query generation time at the tradeoff of a slight increase in code verbosity.


1 Answers

Calling session.expire_all() will evict all database-loaded data from the session. Any access of object attributes subsequent emits a new SELECT statement and gets new data back. Please see http://docs.sqlalchemy.org/en/latest/orm/session_state_management.html#refreshing-expiring for background.

If you still see so-called "caching" after calling expire_all(), then you need to close out transactions as described in my answer linked above.

like image 141
zzzeek Avatar answered Oct 08 '22 22:10

zzzeek