How to disable SQLAlchemy caching?(如何禁用 SQLAlchemy 缓存?)
问题描述
我在使用 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 缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何禁用 SQLAlchemy 缓存?
基础教程推荐
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
- 是否可以执行按位分组功能? 2021-01-01
- 无法解决整理冲突 2021-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- 在 SQL 中连接多个表 2021-01-01
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
