同步交易会相互干扰吗?的PHP/MySQL的 [英] Could simultaneous transactions interfere with each other? php/mysql

查看:95
本文介绍了同步交易会相互干扰吗?的PHP/MySQL的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我找到了这个示例代码,并计划为我的一个项目做类似的事情,但是有一个困扰我的问题

So I found this example code and plan to do something like it for a project of mine but there's a concern that's bugging me

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用户?

the front end uses one mysql user (lets say 'webaccess-admin' 'password') to connect to the database, so what happens if two people run the code at once, and it fails for one of them? would an error in the execution of one instance of a php script cause the sql queries of another instance of that script to rollback as well? do I have to use named transactions or something? or separate mysql users?

这是我正在谈论的一个例子:

Here's an example of what I'm talking about:

person1->开始交易

person1 -> begin transaction

person2->开始交易

person2 -> begin transaction

//两个用户都执行的sql代码

//sql code executed by both users

person1->提交

person1 -> commit

person1 [!错误]

person1 [!error]

person2->仍在运行查询(或可能已经提交)

person2 -> still running queries (or maybe has committed by now)

person1将触发回滚,person 2运行的查询会如何处理?

person1 will trigger a rollback, what happens to the queries that person 2 has run?

推荐答案

交易旨在实现原子操作,也就是说,任何给定的进程都将看到全部交易,或全部都看不到.这样,在您的示例中,person1的事务中的错误不会出现在person2上,回滚也不会影响person2的行为.

Transactions are intended to be atomic, that is, any given process will see all of a transaction, or none of it. In your example, then, an error in person1's transaction will not appear to person2, nor will the rollback affect what person2 does.

但是,有一些警告.例如,仅对于非混合表类型以及表引擎支持的原子性,才能保证原子性.有关完整的详细信息,请访问 MySQL网站

There are some caveats, however. For example, the atomicity is only guaranteed for non-mixed table types, and where table engines support it. The full details are available on the MySQL site

这篇关于同步交易会相互干扰吗?的PHP/MySQL的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆