Php 红豆';s事务不工作
我今天在这里是因为我无法找出使用RedbeanPHP的事务有什么问题。我想问题在于MySQL的“自动提交”值,因为它总是打开的 长话短说:Php 红豆';s事务不工作,php,mysql,transactions,redbean,Php,Mysql,Transactions,Redbean,我今天在这里是因为我无法找出使用RedbeanPHP的事务有什么问题。我想问题在于MySQL的“自动提交”值,因为它总是打开的 长话短说: 1) R::冻结(true)已发布, 2) 尝试了两种R::begin()R::commit()和R::transaction($callback)语法 下面是一个带有测试代码的简单类: class TestTransactions{ public function testme($args){ $tstname = $args; $sr
1)
R::冻结(true)代码>已发布,
2) 尝试了两种R::begin()
R::commit()
和R::transaction($callback)
语法
下面是一个带有测试代码的简单类:
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
取决于我运行脚本的时刻
我错过了什么
谢谢你从不同的角度看待这个问题,我发现了我所缺少的东西
如另一篇文章所述,使用MySQL是不适用的。处理同步访问(作为互斥)的部分代码是必须的
留下这个问题,因为我认为它对其他程序员有用
干杯这个链接实际上与您遇到的问题无关。bean的问题在于,数据是从数据库中提取出来的,然后更新值,最后将新值放回数据库中。在更经典的RDBMS操作中,您只需执行“更新测试集alivesince=alivesince+1 WHERE…”。RedBeans方法防止任何并发控制,除非在检索数据之前启动事务,这不仅效率低下,而且对代码来说很烦人(而且很难维护)。