【发布时间】:2013-08-02 01:42:13
【问题描述】:
所以我找到了这个示例代码,并计划为我的一个项目做类似的事情,但有一个问题困扰着我
try {
// First of all, let's begin a transaction
$db->beginTransaction();
// A set of queries; if one fails, an exception should be thrown
$db->query('first query');
$db->query('second query');
$db->query('third query');
// If we arrive here, it means that no exception was thrown
// i.e. no query has failed, and we can commit the transaction
$db->commit();
} catch (Exception $e) {
// An exception has been thrown
// We must rollback the transaction
$db->rollback();
}
前端使用一个mysql用户(比如说'webaccess-admin''password')连接到数据库,那么如果两个人同时运行代码,其中一个人失败了会发生什么?执行 php 脚本的一个实例时出错会导致该脚本的另一个实例的 sql 查询也回滚吗?我必须使用命名交易或其他东西吗?还是单独的mysql用户?
下面是我所说的一个例子:
person1 -> 开始交易
person2 -> 开始交易
//两个用户都执行的sql代码
person1 -> 提交
person1 [!error]
person2 -> 仍在运行查询(或者现在可能已经提交)
person1 会触发回滚,person 2 运行的查询会发生什么?
【问题讨论】:
标签: php mysql transactions