簡體   English   中英

Redbean的交易無效

[英]Redbean's transaction not working

我今天在這里,因為我無法弄清楚使用RedbeanPHP的交易有什么問題。 我猜問題就在於MySQL的'autocommit'值,因為它始終是ON。

長話短說:
1) R::freeze(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。 處理同步訪問(作為互斥鎖)的代碼的一部分是必須的。

離開這里的問題,因為我認為它對其他程序員有用。

干杯

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM