我目前在封裝PDO事務方面存在問題,易于使用;執行事務後,沒有發生資料庫更改!我的想法是隻提供在PDO事務中執行所需的參數和可調用事務.受保護的executeTransaction方法定義PDO事務的封裝,如下所示.用戶端方法是getNextWidWithLock,它使用executeTransaction方法.我試圖将交易從關閉中移出來并且有效!可能是為什麼它似乎沒有将更改送出到資料庫的原因.
class ParentRepository
{ ...
protected function executeTransaction(Array $data, Callable $transaction)
{
$returnVariable = false;
try
{
$this->mySqlConnect->beginTransaction();
$returnVariable = $transaction($data);
$this->mySqlConnect->commit();
}
catch(\PDOException $e)
{
$this->mySqlConnect->rollBack();
// Log errors.
$dateTime = date("Y-m-d H:i", time());
self::log(sprintf("Error Transaction @ %s" . PHP_EOL, $dateTime));
self::log(sprintf("[Error Message] => %s %s" . PHP_EOL, PHP_EOL, $e->getMessage()));
self::log(sprintf("[File] => %s" . PHP_EOL, $e->getFile()));
self::log(sprintf("[Line Number] => %s" . PHP_EOL, $e->getLine()));
self::log(sprintf("[Back Trace] => %s %s" . PHP_EOL, PHP_EOL, $e->getTraceAsString()));
self::log("______________________________________" . PHP_EOL);
throw $e;
}
return $returnVariable;
}
protected function getNextWidWithLock($tableName, $branchId, $reservedSlots = 1)
{
if ($reservedSlots === 0)
return [];
$this->executeTransaction(
[
'tableName' => $tableName,
'branchId' => $branchId,
'reservedSlots' => $reservedSlots
],
function($params)
{
extract($params);
$minimumWid = $branchId . "0000000";
$maximumWid = $branchId . "9999999";
$preparedStatements = array(
$this->mySqlConnect->prepare("SET @wid_d = 0"),
$this->mySqlConnect->prepare("SELECT COALESCE(MAX(`wid`), $minimumWid) INTO @wid_d FROM $tableName
WHERE `wid` >= $minimumWid AND `wid` <= $maximumWid FOR UPDATE"),
$this->mySqlConnect->prepare("INSERT INTO $tableName(`wid`)
VALUES ( IF(@wid_d = 0, $minimumWid + 1 , @wid_d + $reservedSlots) )")
);
foreach ($preparedStatements as $statement)
$statement->execute();
$result = $this->mySqlConnect->prepare("SELECT `wid` FROM $tableName WHERE id = " . $this->mySqlConnect->lastInsertId());
$result->execute();
$end = $result->fetchColumn();
$statement = $this->mySqlConnect->prepare("SELECT IF(@wid_d = 0, $minimumWid + 1 , @wid_d + 1)");
$statement->execute();
$begin = $statement->fetchColumn();
return range($begin, $end);
}
);
}
}
解決方法:
你在關閉時使用$this,但它從PHP 5.3開始是not supporting
你可以寫這樣的東西:
protected function getNextWidWithLock($tableName, $branchId, $reservedSlots = 1)
{
if ($reservedSlots === 0)
return [];
$self = $this;
$this->executeTransaction(
[
'tableName' => $tableName,
'branchId' => $branchId,
'reservedSlots' => $reservedSlots
],
function($params) use ($self, $tableName, $branchId, $reservedSlots)
{
extract($params);
$minimumWid = $branchId . "0000000";
$maximumWid = $branchId . "9999999";
$preparedStatements = array(
$self->mySqlConnect->prepare("SET @wid_d = 0"),
$self->mySqlConnect->prepare("SELECT COALESCE(MAX(`wid`), $minimumWid) INTO @wid_d FROM $tableName
WHERE `wid` >= $minimumWid AND `wid` <= $maximumWid FOR UPDATE"),
$self->mySqlConnect->prepare("INSERT INTO $tableName(`wid`)
VALUES ( IF(@wid_d = 0, $minimumWid + 1 , @wid_d + $reservedSlots) )")
);
foreach ($preparedStatements as $statement)
$statement->execute();
$result = $self->mySqlConnect->prepare("SELECT `wid` FROM $tableName WHERE id = " . $self->mySqlConnect->lastInsertId());
$result->execute();
$end = $result->fetchColumn();
$statement = $self->mySqlConnect->prepare("SELECT IF(@wid_d = 0, $minimumWid + 1 , @wid_d + 1)");
$statement->execute();
$begin = $statement->fetchColumn();
return range($begin, $end);
}
);
}
}
标簽:php,mysql,pdo,transactions
來源: https://codeday.me/bug/20190702/1357089.html