gpt4 book ai didi

php - Redbean 的交易无效

转载 作者:可可西里 更新时间:2023-11-01 09:02:21 26 4
gpt4 key购买 nike

我今天来这里是因为我无法弄清楚我使用 RedbeanPHP 进行的交易有什么问题。我猜问题出在 MySQL 的“autocommit”值上,因为它始终处于打开状态。

长话短说:
1) R::freeze(true); 已发出,
2) 尝试了 R::begin() .. R::commit()R::transaction($callback) 语法< br/>

这是一个带有测试代码的简单类:

class TestTransactions{
public function testme($args){
$tstname = $args;
$src = R::findOne('batchscripts', 'batchclass = :tstname',
array(':tstname' => $tstname));

sleep(2);

if($src){
$src->alivesince = intval($src->alivesince) + 1;
R::store($src);
} else {
$bean = R::dispense('batchscripts');
$bean->batchclass = $tstname;
$bean->alivesince = 0;
$bean->start = R::$f->now();
R::store($bean);
}
}

public function testCallback(){
$that = &$this;
R::freeze(true);
try{
$ret2 = R::transaction(function() use ($that){
//uncomment me to see autocommit value
//$g = R::getAll("show global variables like 'autocommit'");
//$g = array_pop($g);
//var_dump($g);
$that->testme('instance');
});
} catch (Exception $e){
throw $e;
}

}

public function testProcedural(){
R::freeze(true);
try{
R::begin();
$this->testme('instance2');
R::commit();
} catch (Exception $e) {
R::rollback();
throw $e;
}

}

public function test(){

$this->testCallback();
$this->testProcedural();

}
}

同时运行带有更多 PHP 脚本的 test() 函数(我试过 12 个),数据库条目不正确:

我希望有

batchclass: 'instance',  alivesince: 11
batchclass: 'instance2', alivesince: 11

相反,我得到了

batchclass: 'instance',  alivesince: 7
batchclass: 'instance2', alivesince: 7

甚至

batchclass: 'instance',  alivesince: 5
batchclass: 'instance2', alivesince: 5

取决于我运行脚本的时刻。

我在这里错过了什么?

谢谢

最佳答案

从不同的角度看问题,我找到了我遗漏的东西。

如另一篇文章所述 a multithread transaction不适用于使用 MySQL。处理同步访问(作为互斥锁)的部分代码是必须的。

把这个问题留在这里,因为我认为它对其他程序员很有用。

干杯

关于php - Redbean 的交易无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37566780/

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