如何禁用SQLAlchemy缓存? [英] How to disable SQLAlchemy caching?

查看:176
本文介绍了如何禁用SQLAlchemy缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用sqlalchemy时出现缓存问题.

我使用sqlalchemy将数据插入MySQL数据库.然后,我有另一个应用程序处理此数据,并直接对其进行更新.

但是sqlalchemy总是返回旧数据而不是更新数据.我认为sqlalchemy缓存了我的请求...所以...我应该如何禁用它?

解决方案

人们通常认为,除了在事务本地使用的常规SQLAlchemy身份映射之外,还有一个缓存"正在起作用,这是因为他们正在观察事务隔离的影响.默认情况下,SQLAlchemy的会话在事务模式下工作,这意味着它一直等到session.commit()被调用才能将数据持久保存到数据库中.在这段时间内,其他地方正在进行的其他交易将看不到此数据.

但是,由于交易具有隔离性,因此存在额外的变数.其他正在进行的交易不仅会在提交之前看不到您的交易数据,而且在某些情况下,甚至在他们提交或回滚之前,它们也不会看到(这与您的交易具有相同的效果) close()在这里).平均程度为 isolation 的事务将保持其到目前为止已加载的状态,并且即使真实数据已更改,也将继续为您提供该事务本地的相同状态-这称为<用事务隔离的话来说,strong>可重复读取.

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缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆