innodb 中的并发

发布于 2024-11-16 13:05:15 字数 338 浏览 7 评论 0原文

我有一个这样的代码

Reserve.php

$r=mysql_query("select count(*) from ticket");
$rec=mysql_fetch_array($r);
if ($rec[0]==0)
{
insert into ticket values .....
}

我只有 1 张票。 两个用户请求reserve.php。

“a”用户请求 Reserve.php 且可用票证为 0 。但在插入之前,对于“b”用户,可用票证还为 0。所以两个用户预订票。

表是Innodb。 如何防止这种情况?事务、锁表还是什么?

i have a code like this

reserve.php

$r=mysql_query("select count(*) from ticket");
$rec=mysql_fetch_array($r);
if ($rec[0]==0)
{
insert into ticket values .....
}

i have 1 ticket only.
two users request reserve.php.

"a" user request reserve.php and available ticket is 0 . but before insert, for "b" user available ticket is 0 yet. so two users reserve ticket.

table is Innodb.
how to prevent this? transaction , lock table or what?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

奶茶白久 2024-11-23 13:05:15

在这些情况下,我通常只使用 UPDATE 语句并检查有多少记录受到更新的影响 (mysql_affected_rows)。

 UPDATE tickets SET ticket_count=ticket_count-1 WHERE ticket_id=123 AND ticket_count>0

如果其他人首先递减计数器,则不会发生更新,并且您不会向用户提供票证。应启用自动提交,以便更新是一个独立的“事务”。

或者,您可以将 SELECT 更改为 SELECT ... FOR UPDATE
http://dev.mysql.com/doc/refman /5.0/en/innodb-locking-reads.html

In these situations I usually just use an UPDATE statement and check how many records were affected (mysql_affected_rows) by the update.

 UPDATE tickets SET ticket_count=ticket_count-1 WHERE ticket_id=123 AND ticket_count>0

If someone else decremented the counter first, then no update occurs and you don't give the user a ticket. Autocommit should be enabled so the update is a self-contained "transaction".

Alternatively, you can change your SELECT to be SELECT ... FOR UPDATE
http://dev.mysql.com/doc/refman/5.0/en/innodb-locking-reads.html

花想c 2024-11-23 13:05:15

我会在单个事务中完成此操作,无需往返应用程序级别 - SELECT count() ....INSERT INTO 均在单个命令中发送来自 PHP 并嵌入到带有 EXCLUSIVE LOCKTRANSACTION

I would do it in a single transaction, without a roundtrip back to the application level - both SELECT count() .... and INSERT INTO being sent in a single command from PHP and embedded into a TRANSACTION with EXCLUSIVE LOCK

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文