关于在sqlalchemy会话中刷新对象 [英] About refreshing objects in sqlalchemy session

查看:105
本文介绍了关于在sqlalchemy会话中刷新对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我正在处理有关sqlalchemy和对象刷新的疑问!

Well, I am dealing with a doubt about sqlalchemy and objects refreshing!

我处于2个会话的状态,并且在两个会话中都查询了相同的对象!...对于某些特定的事情,我无法关闭其中一个会话. 我已经修改了该对象并在会话A中提交了更改,但是在会话B中,属性是初始属性!未经修改!..

I am in the situation in what I have 2 sessions, and the same object has been queried in both sessions!... For some particular thing I cannot to close one of the sessions. I have modified the object and commited the changes in session A, but in session B, the attributes are the initial ones! without modifications!..

所以...我应该实现某种通知系统来传达更改,还是在sqlalchemy中有内置的方式来做到这一点?

So... shall I implement some kind of notification system to communicate changes or there is a built-in way to do this in sqlalchemy??

推荐答案

Sessions are designed to work like this. The attributes of the object in Session B WILL keep what it had when first queried in Session B. Additionally, SQLAlchemy will not attempt to automatically refresh objects in other sessions when they change, nor do I think it would be wise to try to create something like this.

您应该将每个会话的寿命视为数据库中的单个事务.会话如何以及何时需要处理其对象可能已过时这一事实,不是可以通过SQLAlchemy(或SQLAlchemy的任何扩展)中内置的算法解决的技术问题:这是一个业务"问题,您必须解决该问题确定并编写自己的代码. 正确"的响应可能是说这不是问题:如果会话B在会话B启动时使用了数据,则会话B发生的逻辑可能是有效的.您的问题"实际上可能不是问题.这些文档实际上具有

You should be actively thinking of the lifespan of each session as a single transaction in the database. How and when sessions need to deal with the fact that their objects might be stale is not a technical problem that can be solved by an algorithm built into SQLAlchemy (or any extension for SQLAlchemy): it is a "business" problem whose solution you must determine and code yourself. The "correct" response might be to say that this isn't a problem: the logic that occurs with Session B could be valid if it used the data at the time that Session B started. Your "problem" might not actually be a problem. The docs actually have an entire section on when to use sessions, but it gives a pretty grim response if you are hoping for a one-size-fits-all solution...

会话通常在逻辑的开始处构建 可能需要数据库访问的操作.

A Session is typically constructed at the beginning of a logical operation where database access is potentially anticipated.

无论何时会话用于与数据库对话,它都会开始 数据库事务一开始就开始通信.假设 autocommit标志保留其建议的默认值False,这是 交易一直进行到会话回滚为止, 提交或关闭.会话将开始新的交易 在上一个事务结束之后再次使用;从 因此,该会话具有一定的使用寿命 跨很多交易,尽管一次只能进行一次.我们参考这些 事务范围和会话范围这两个概念.

The Session, whenever it is used to talk to the database, begins a database transaction as soon as it starts communicating. Assuming the autocommit flag is left at its recommended default of False, this transaction remains in progress until the Session is rolled back, committed, or closed. The Session will begin a new transaction if it is used again, subsequent to the previous transaction ending; from this it follows that the Session is capable of having a lifespan across many transactions, though only one at a time. We refer to these two concepts as transaction scope and session scope.

此处的含义是SQLAlchemy ORM鼓励 开发人员在他或她的应用程序中建立这两个范围, 不仅包括范围开始和结束的时间,还包括范围 这些范围中的一个,例如单个Session实例是否应该是本地的 函数或方法中的执行流,应该是 整个应用程序使用的全局对象,或者介于两者之间的某个位置 这两个.

The implication here is that the SQLAlchemy ORM is encouraging the developer to establish these two scopes in his or her application, including not only when the scopes begin and end, but also the expanse of those scopes, for example should a single Session instance be local to the execution flow within a function or method, should it be a global object used by the entire application, or somewhere in between these two.

开发人员确定此范围的负担是一个领域 SQLAlchemy ORM必定对如何 应该使用数据库.具体的工作模式单位 一种是随着时间的推移累积更改并定期刷新它们, 保持内存状态与已知状态同步 本地交易.此模式仅在有意义时有效 交易范围就位.

The burden placed on the developer to determine this scope is one area where the SQLAlchemy ORM necessarily has a strong opinion about how the database should be used. The unit of work pattern is specifically one of accumulating changes over time and flushing them periodically, keeping in-memory state in sync with what’s known to be present in a local transaction. This pattern is only effective when meaningful transaction scopes are in place.

也就是说,您可以做一些事情来改变情况的工作方式:

That said, there are a few things you can do to change how the situation works:

首先,您可以减少会话保持打开状态的时间.会话B正在查询对象,然后稍后您要对该对象(在同一会话中)执行某些操作,以使属性保持最新状态.一种解决方案是在单独的会话中完成第二个操作.

First, you can reduce how long your session stays open. Session B is querying the object, then later you are doing something with that object (in the same session) that you want to have the attributes be up to date. One solution is to have this second operation done in a separate session.

另一种方法是使用expire/refresh方法,如文档显示 ...

Another is to use the expire/refresh methods, as the docs show...

# immediately re-load attributes on obj1, obj2
session.refresh(obj1)
session.refresh(obj2)

# expire objects obj1, obj2, attributes will be reloaded
# on the next access:
session.expire(obj1)
session.expire(obj2)

您可以使用session.refresh()立即获取对象的最新版本,即使会话早先已经查询了该对象.

You can use session.refresh() to immediately get an up-to-date version of the object, even if the session already queried the object earlier.

这篇关于关于在sqlalchemy会话中刷新对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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