SQLAlchemy如何强制开始

时间:2018-07-30 17:20:56

标签: flask sqlalchemy flask-sqlalchemy

背景:Apache服务器使用mod_wsgi通过Flask_Sqlalchemy连接到MySQL为Flask应用提供服务。这是一个全栈应用程序,因此几乎不可能创建一个最小的示例,但是我已经尝试过了。

我的问题是,当我进行应修改数据库的某些更改时,后续请求似乎并不总是反映出该更改。例如,如果我创建一个对象,然后尝试编辑该对象,则编辑有时会失败。

当打开SQLALCHEMY_ECHO和SQLALCHEMY_COMMIT_ON_TEARDOWN时,我可以看到,当响应良好时,一切都由BEGIN和COMMIT构成。但是当响应不佳时,这些东西就会丢失。

我为为什么没有发生BEGIN而感到困惑,因此有一种解决方法可以强制BEGIN?所有文档都说不要这样做,但是如果没有发生

有时,GET将返回正确的信息,但有时会返回以前的信息。 输出示例:

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

当请求正确时,通常使用第一个日志片段。您可以在拆卸时看到隐式的BEGIN和COMMIT。

[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

下一个代码段通常用于请求不正确的情况。您可以看到它与以前一样执行SELECT,但是没有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 ---

0 个答案:

没有答案