在我的代码中,我启动了一个事务,并插入了一些数据。
然后,我可以使用select来获取刚才在事务提交之前插入的行。
怎么会出这事?
我的数据库是带有Innodb引擎的Mysql。
谢谢!
发布于 2015-12-23 21:53:44
正如我在评论中所说的,事务就是这样工作的。
可能您正在执行您的SELECT在您以前用于INSERT数据的事务中,因此即使尚未提交,这些数据实际上仍然存在于该事务中。
您可以尝试在另一个事务中移动SELECT,或者使用它作为独立语句(这仍然是幕后的另一个事务),这样您就看不到数据了。
发布于 2015-12-23 22:03:06
如前所述,事务就是这样工作的。即使还没有提交结果,执行它们的过程也会“看到”结果。但是,在提交之前,其他进程不会看到它们。(也就是说,如果另一个用户登录到DB中&查询这些行,则在您实际提交它们之前,它们不会找到它们)。
发布于 2015-12-23 07:31:02
默认情况下,MySQL工作在自动提交模式-在超越所有更新操作(插入/更新/插入)自动完成。但你可以用:
SET autocommit=0;要了解更多细节,您可以阅读文档:http://dev.mysql.com/doc/refman/5.7/en/commit.html
To disable autocommit mode explicitly, use the following statement:
SET autocommit=0;
After disabling autocommit mode by setting the autocommit variable to zero, changes to transaction-safe tables (such as those for InnoDB or NDB) are not made permanent immediately. You must use COMMIT to store your changes to disk or ROLLBACK to ignore the changes.
autocommit is a session variable and must be set for each session. To disable autocommit mode for each new connection, see the description of the autocommit system variable at Section 5.1.4, “Server System Variables”.https://stackoverflow.com/questions/34430613
复制相似问题