gpt4 book ai didi

php - Laravel DB::beginTransaction 导致其他事务被锁定

转载 作者:行者123 更新时间:2023-11-29 02:44:48 25 4
gpt4 key购买 nike

在经典的 php 中我有 2 个进程文件

1) process1.php => 锁定 .table InnodB 的行。

2) process2.php => 从同一张表中读取另一行

我已经在 PHP/MySQL 中使用了 START TRANSACTION AND COMMIT 并且它按我想要的方式正常工作。我想要的是 process2.php 可以选择另一条记录,即使 process1.php 仍在运行并锁定某些行。

不幸的是,它在 Laravel 中不起作用,在 Laravel 中我的其他进程被锁定,直到第一个进程完成,尽管第二个进程正在选择另一个表。

请帮我解决这个问题。

DB::beginTransaction();
$rs = DB::table('test_iap')->where('content', 'waiting')->lockForUpdate()->get();

$sql = "update test_iap set content='Updated' where id=1";
DB::connection('mysql_pencil_main')->update(DB::raw($sql));
sleep(10);# To allow testing at different file process

DB::commit();

最佳答案

使用 sharedLock() 而不是 lockForUpdate()。共享锁可防止选定的行在您的事务提交之前被修改。查看更多描述 here

DB::beginTransaction();
$rs = DB::table('test_iap')->where('content', 'waiting')->sharedLock()->get();

$sql = "update test_iap set content='Updated' where id=1";
DB::connection('mysql_pencil_main')->update(DB::raw($sql));
sleep(10);# To allow testing at different file process

DB::commit();

关于php - Laravel DB::beginTransaction 导致其他事务被锁定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44469780/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com