简体   繁体   English

MySQL和Python:从一个线程提交对另一个线程不可见,但在MySQL Workbench中可见

[英]MySQL and Python: commits from one thread not visible to another thread, but visible from MySQL Workbench

I have two Python threads accessing the same MySQL database, each with its own connection and cursor object. 我有两个Python线程访问同一个MySQL数据库,每个线程都有自己的连接和游标对象。 One thread inserts a record into a table. 一个线程将一条记录插入表中。 As I would expect, from MySQL Workbench, I don't see the table grow in length until the writing thread does a commit, then I see the change. 正如我所期望的那样,从MySQL Workbench来看,直到写入线程进行了提交,我才看到表的长度增加,然后才看到更改。 The problem is, my other thread doesn't see the change: it sees the old state of the table, as if the commit never happened. 问题是,我的另一个线程看不到更改:它看到了表的旧状态,好像提交从未发生过。 When I restart the application, the length of the table does get read correctly, but remains stuck there for all eternity, no matter how many records the writing thread appends and commits. 当我重新启动应用程序时,表的长度确实可以正确读取,但是无论写线程追加和提交多少条记录,它都永远卡在那儿。 I feel like I'm missing something really obvious here. 我觉得我在这里确实缺少一些明显的东西。

To read the length of the table, I've tried: select count(*) from mystupidtable select count(id) from mystupidtable and even: select id from mystupidtable 要读取表的长度,我尝试过:从mystupidtable中选择count(*)从mystupidtable中选择count(id)甚至:从mystupidtable中选择id

All these attempts return the old state of the table, as though the commit never happened. 所有这些尝试都会返回表的旧状态,好像提交从未发生过。

It sounds like the 2nd thread is running off a snapshot of the database from when it implicitly opened a transaction. 听起来好像第二个线程正在从隐式打开事务时开始运行数据库快照。 (Which I think cursors do.) If so, committing the cursor on the 2nd thread should allow it to see the new data. (我认为游标的。)如果是这样,则在第二个线程上提交游标将使它能够查看新数据。

See https://dev.mysql.com/doc/refman/5.0/en/innodb-consistent-read.html and the REPEATABLE READ level. 参见https://dev.mysql.com/doc/refman/5.0/en/innodb-consistent-read.html和REPEATABLE READ级别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM