问题描述
我在使用 sqlalchemy
时遇到缓存问题.
我使用 sqlalchemy
将数据插入到 MySQL 数据库中.然后,我有另一个应用程序处理这些数据,并直接更新它.
但是 sqlalchemy
总是返回旧数据而不是更新的数据.我认为 sqlalchemy
缓存了我的请求......所以......我应该如何禁用它?
人们认为有一个缓存"在起作用的常见原因,除了通常的 SQLAlchemy 身份映射是本地的事务之外,是他们正在观察事务隔离的影响.SQLAlchemy 的会话默认在事务模式下工作,这意味着它会等到 session.commit()
被调用以将数据持久化到数据库.在此期间,其他地方正在进行的其他交易将看不到此数据.
然而,由于交易的孤立性质,还有一个额外的转折.其他正在进行的事务不仅在提交之前不会看到您的事务的数据,而且在某些情况下,在它们被提交或回滚之前他们也无法看到它(这与您的close() 在这里).具有平均隔离程度的事务将保持其迄今为止加载的状态,并继续为您提供事务本地的相同状态,即使实际数据已更改 - 这称为 <强>可重复读取在事务隔离的说法中.
http://en.wikipedia.org/wiki/Isolation_%28database_systems%29>
I have a caching problem when I use sqlalchemy
.
I use sqlalchemy
to insert data into a MySQL database. Then, I have another application process this data, and update it directly.
But sqlalchemy
always returns the old data rather than the updated data. I think sqlalchemy
cached my request ... so ... how should I disable it?
The usual cause for people thinking there's a "cache" at play, besides the usual SQLAlchemy identity map which is local to a transaction, is that they are observing the effects of transaction isolation. SQLAlchemy's session works by default in a transactional mode, meaning it waits until session.commit()
is called in order to persist data to the database. During this time, other transactions in progress elsewhere will not see this data.
However, due to the isolated nature of transactions, there's an extra twist. Those other transactions in progress will not only not see your transaction's data until it is committed, they also can't see it in some cases until they are committed or rolled back also (which is the same effect your close() is having here). A transaction with an average degree of isolation will hold onto the state that it has loaded thus far, and keep giving you that same state local to the transaction even though the real data has changed - this is called repeatable reads in transaction isolation parlance.
http://en.wikipedia.org/wiki/Isolation_%28database_systems%29
这篇关于如何禁用 SQLAlchemy 缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!