为什么我可以获得未提交的行?
why can I get an uncommitted row?
在我的代码中,我启动了一个事务,并插入了一些数据。
然后我可以使用 select
来获取我刚才在事务提交之前插入的行。
这是怎么发生的?
我的数据库是 Mysql 引擎 Innodb。
谢谢!
默认情况下 MySQL 在自动提交模式下工作 - 在交易中所有更新操作 (INSERT/UPDATE/INSERT) 都会自动提交。但是你可以使用:
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”.
正如我在评论中所说,这就是交易的工作方式。
可能您正在执行您的 SELECT
在您用于 INSERT
您的数据的同一事务中,因此这些数据实际上存在于该事务中,即使尚未提交。
您可以尝试在另一个事务中移动 SELECT
或将其用作独立语句(这仍然是引擎盖下的另一个事务),您将不会再看到数据。
如前所述,这就是交易的运作方式。执行它们的过程 'sees' 结果,即使它们尚未提交。但是,在它们被提交之前,其他进程将看不到它们。 (即,如果另一个用户登录到数据库并查询行,他们将在您实际提交它们之前找不到它们)。
在我的代码中,我启动了一个事务,并插入了一些数据。
然后我可以使用 select
来获取我刚才在事务提交之前插入的行。
这是怎么发生的?
我的数据库是 Mysql 引擎 Innodb。
谢谢!
默认情况下 MySQL 在自动提交模式下工作 - 在交易中所有更新操作 (INSERT/UPDATE/INSERT) 都会自动提交。但是你可以使用:
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”.
正如我在评论中所说,这就是交易的工作方式。
可能您正在执行您的 SELECT
在您用于 INSERT
您的数据的同一事务中,因此这些数据实际上存在于该事务中,即使尚未提交。
您可以尝试在另一个事务中移动 SELECT
或将其用作独立语句(这仍然是引擎盖下的另一个事务),您将不会再看到数据。
如前所述,这就是交易的运作方式。执行它们的过程 'sees' 结果,即使它们尚未提交。但是,在它们被提交之前,其他进程将看不到它们。 (即,如果另一个用户登录到数据库并查询行,他们将在您实际提交它们之前找不到它们)。