使用PHP PDO进行的事务如何与并发一起工作? [英] How exactly do transactions with PHP PDO work with concurrency?

查看:116
本文介绍了使用PHP PDO进行的事务如何与并发一起工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个Web应用程序,在该应用程序中,他们将是多个用户彼此交互并阅读/做出/修改共享数据的决定.

I'm making a webapp where they'll be multiple users interacting with each other and reading/making decisions on/modifying shared data.

我已经读到交易是原子的,这就是我所需要的.但是,我不确定它如何与PHP PDO::beginTransaction()

I've read that transactions are atomic, which is what I need. However, I'm not sure how it works with the PHP PDO::beginTransaction()

我的意思是原子的,就像一个事务正在编辑某些数据一样,所有其他事务也在修改/读取该数据,需要等到第一个事务完成为止.就像我不想让两个脚本读取一个值,递增旧的脚本并有效地仅存储一个增量一样.第二个脚本应等待第一个脚本完成.

I mean atomic as in if one transaction is editing some data, all other transactions also modifying/reading that data will need to wait until the first transaction finishes. Like I don't want two scripts reading a value, incrementing the old one, and effectively storing only one increment. The second script should have to wait for the first one to finish.

在我看到的几乎所有示例中,查询都是连续使用的(示例 PHP + MySQL事务示例).我正在做的很多事情都需要

In almost all the examples I've seen the queries are used consecutively (example PHP + MySQL transactions examples). A lot of what I'm doing requires

  • 查询和提取
  • 在同一笔交易中检查该数据并对其进行操作
  1. 如果查询之间存在PHP代码,事务是否仍将自动运行?
  2. 我知道您应该在交易外部准备您的对帐单,但是可以在交易内部准备对帐单吗?基本上,我担心PHP活动会破坏事务的原子性.
  1. Will the transaction still work atomically if there is PHP code between queries?
  2. I know you should prepare your statements outside the transaction, but is it okay to prepare it inside? Basically, I'm worried about PHP activity ruining the atomicity of a transaction.

这是一个示例(此示例不需要检查先前的值).我有一个非常基本的收件箱系统,将邮件存储为序列化数组(如果有人有更好的建议,请告诉我).因此,我对其进行查询,追加新消息并进行存储.它会按预期工作吗?

Here's an example (this one doesn't require checking a previous value). I have a very basic inbox system which stores mail as a serialized array (if someone has a better recommendation please let me know). So I query for it, append the new message, and store it. Will it work as expected?

$getMail = $con->prepare('SELECT messages FROM inboxes WHERE id=?');
$storeMail = $con->prepare('UPDATE inboxes SET messages=? WHERE id=?');
$con->beginTransaction();
$getMail->execute(array($recipientID));
$result = $getMail->fetch();
$result = unserialize($result[0]);
$result[] = $msg;
$storeMail->execute(array(serialize($result), $recipientID));
$con->commit();

推荐答案

仅对于尝试使用相同数据的其他数据库连接而言,事务是原子的,即,其他连接将看到 no 所做的更改根据您的交易或所有的更改; 原子"表示没有其他数据库连接会处于中间状态,其中某些数据已更新,而另一些则没有.

Transactions are atomic only with respect to other database connections trying to use the same data, i.e. other connections will see either no changes made by your transaction, or all changes; "atomic" meaning no other database connection will see an in-between state with some data updated and other not.

查询之间的PHP代码不会破坏原子性,并且在哪里准备语句都无关紧要.

PHP code between queries won't break atomicity, and it does not matter where you prepare your statements.

这篇关于使用PHP PDO进行的事务如何与并发一起工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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