sqlalchemy缺少begin似乎会导致会话不同步

sf6xfgos  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(378)

更新到我的最后一个问题,请阅读这篇文章的背景信息。这是我尝试过的红色线。我已经做了进一步的调查。
热释光;博士说,对于一些请求,我似乎得到了旧数据。我通过发送一个post来更新一个对象并打印该对象的一个属性来测试这一点。通常情况下,React是好的,但我经常得到旧数据。当创建与其他对象有关系的对象时,这是一个大问题,如果由于某种原因外键对象不存在,创建将失败。
例子:

format@pixel:~$ curl -X POST https://example.com/app/region/rename/5/Old
Success
format@pixel:~$ curl -X POST https://example.com/app/region/rename/5/New
Success
format@pixel:~$ for i in {1..25}; do  echo ; curl -X GET https://example.com/app/region/name/5/; done
New
New
New
New
New
New
Old
New
New
New
New
New
New
New
New
New
New
New
New
New
New
New
New
New
New

我打开了sqlalchemy\u echo和sqlalchemy\u commit\u on\u teardown

app.config['SQLALCHEMY_ECHO'] = True
app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True

并更改了代码以打印更多信息。
当请求正确时,第一个日志片段是典型的。您可以看到隐式begin和commit on teardown。

[Tue Jul 24 17:49:23.217666 2018] [wsgi:error] [pid 27919:tid 139730801542912] --- Get Start ---
[Tue Jul 24 17:49:23.219964 2018] [wsgi:error] [pid 27919:tid 139730801542912] 2018-07-24 17:49:23,219 INFO sqlalchemy.engine.base.Engine BEGIN (implicit)
[Tue Jul 24 17:49:23.220053 2018] [wsgi:error] [pid 27919:tid 139730801542912] INFO:sqlalchemy.engine.base.Engine:BEGIN (implicit)
[Tue Jul 24 17:49:23.221282 2018] [wsgi:error] [pid 27919:tid 139730801542912] 2018-07-24 17:49:23,221 INFO sqlalchemy.engine.base.Engine SELECT region.id AS region_id, region.name AS region_name, region.north AS region_north, region.west AS region_west, region.south AS region_south, region.east AS region_east, region.coords AS region_coords 
[Tue Jul 24 17:49:23.221321 2018] [wsgi:error] [pid 27919:tid 139730801542912] FROM region 
[Tue Jul 24 17:49:23.221336 2018] [wsgi:error] [pid 27919:tid 139730801542912] WHERE region.id = %s
[Tue Jul 24 17:49:23.221356 2018] [wsgi:error] [pid 27919:tid 139730801542912] 
[Tue Jul 24 17:49:23.221414 2018] [wsgi:error] [pid 27919:tid 139730801542912] INFO:sqlalchemy.engine.base.Engine:SELECT region.id AS region_id, region.name AS region_name, region.north AS region_north, region.west AS region_west, region.south AS region_south, region.east AS region_east, region.coords AS region_coords 
[Tue Jul 24 17:49:23.221433 2018] [wsgi:error] [pid 27919:tid 139730801542912] FROM region 
[Tue Jul 24 17:49:23.221447 2018] [wsgi:error] [pid 27919:tid 139730801542912] WHERE region.id = %s
[Tue Jul 24 17:49:23.221465 2018] [wsgi:error] [pid 27919:tid 139730801542912] 
[Tue Jul 24 17:49:23.221683 2018] [wsgi:error] [pid 27919:tid 139730801542912] 2018-07-24 17:49:23,221 INFO sqlalchemy.engine.base.Engine (5,)
[Tue Jul 24 17:49:23.221767 2018] [wsgi:error] [pid 27919:tid 139730801542912] INFO:sqlalchemy.engine.base.Engine:(5,)
[Tue Jul 24 17:49:23.222922 2018] [wsgi:error] [pid 27919:tid 139730801542912] 2018-07-24 17:49:23.222868 New
[Tue Jul 24 17:49:23.222990 2018] [wsgi:error] [pid 27919:tid 139730801542912] --- Get End ---
[Tue Jul 24 17:49:23.224867 2018] [wsgi:error] [pid 27919:tid 139730801542912] 2018-07-24 17:49:23,224 INFO sqlalchemy.engine.base.Engine COMMIT
[Tue Jul 24 17:49:23.224946 2018] [wsgi:error] [pid 27919:tid 139730801542912] INFO:sqlalchemy.engine.base.Engine:COMMIT

下一个代码段是请求不正确时的典型代码段。您可以看到它正在执行与以前相同的选择操作,但是没有begin或commit。不知道这意味着什么,也不知道为什么会发生,但这让我相信问题出在sqlalchemy或mysql层。

[Tue Jul 24 17:49:23.288116 2018] [wsgi:error] [pid 27919:tid 139730654033664] --- Get Start ---
[Tue Jul 24 17:49:23.290049 2018] [wsgi:error] [pid 27919:tid 139730654033664] 2018-07-24 17:49:23,289 INFO sqlalchemy.engine.base.Engine SELECT region.id AS region_id, region.name AS region_name, region.north AS region_north, region.west AS region_west, region.south AS region_south, region.east AS region_east, region.coords AS region_coords 
[Tue Jul 24 17:49:23.290079 2018] [wsgi:error] [pid 27919:tid 139730654033664] FROM region 
[Tue Jul 24 17:49:23.290092 2018] [wsgi:error] [pid 27919:tid 139730654033664] WHERE region.id = %s
[Tue Jul 24 17:49:23.290109 2018] [wsgi:error] [pid 27919:tid 139730654033664] 
[Tue Jul 24 17:49:23.290168 2018] [wsgi:error] [pid 27919:tid 139730654033664] INFO:sqlalchemy.engine.base.Engine:SELECT region.id AS region_id, region.name AS region_name, region.north AS region_north, region.west AS region_west, region.south AS region_south, region.east AS region_east, region.coords AS region_coords 
[Tue Jul 24 17:49:23.290186 2018] [wsgi:error] [pid 27919:tid 139730654033664] FROM region 
[Tue Jul 24 17:49:23.290197 2018] [wsgi:error] [pid 27919:tid 139730654033664] WHERE region.id = %s
[Tue Jul 24 17:49:23.290214 2018] [wsgi:error] [pid 27919:tid 139730654033664] 
[Tue Jul 24 17:49:23.290422 2018] [wsgi:error] [pid 27919:tid 139730654033664] 2018-07-24 17:49:23,290 INFO sqlalchemy.engine.base.Engine (5,)
[Tue Jul 24 17:49:23.290499 2018] [wsgi:error] [pid 27919:tid 139730654033664] INFO:sqlalchemy.engine.base.Engine:(5,)
[Tue Jul 24 17:49:23.291567 2018] [wsgi:error] [pid 27919:tid 139730654033664] 2018-07-24 17:49:23.291529 @@OLD@@
[Tue Jul 24 17:49:23.291591 2018] [wsgi:error] [pid 27919:tid 139730654033664] --- Get End ---

从时间戳可以看出,好的请求和坏的请求都是紧跟在一起的,在这个坏的请求之后也有几个好的请求,似乎没有押韵或理由。
我正在使用mysqlclient 1.3.13、sqlalchemy 1.2.10、flask sqlalchemy 2.3.2并运行 apt-get upgrade 今天早些时候。

mrphzbgm

mrphzbgm1#

作为参考,如果有人发现这个线程与同一问题,我修复了我的问题。
我的 flask 应用程序工厂功能有线 app.app_context().push() 早期基于 flask 教程的遗留物。在项目的重组过程中,这条线被遗漏了,问题自行解决了。不确定此行为什么或如何导致此问题,并且仅针对部分但不是所有请求。

相关问题