Portál AbcLinuxu, 13. května 2025 00:50
public function beginTransaction() { if ($this->transLevel === 0) { $this->connection->beginTransaction(); } else { $this->connection->exec("SAVEPOINT LEVEL{$this->transLevel}"); } $this->transLevel++; } public function commit() { $this->transLevel--; if ($this->transLevel <= 0) { $this->transLevel = 0; $this->connection->commit(); } else { $this->connection->exec("RELEASE SAVEPOINT LEVEL{$this->transLevel}"); } } public function rollBack() { $this->transLevel--; if ($this->transLevel <= 0) { $this->transLevel = 0; $this->connection->rollBack(); } else { $this->connection->exec("ROLLBACK TO SAVEPOINT LEVEL{$this->transLevel}"); } }V kódu se to používá takto:
for ($raceI = 0; $raceI <= 10; $raceI++) { $inTransaction = false; try { $this->dbConnection->beginTransaction(); $inTransaction = true; ... NEJAKE DOTAZY, KTERE MOHOU ROVNEZ OBSAHOVAT TAKOVE KONSTRUKCE $this->dbConnection->commit(); break; } catch (\PDOException $e) { if ($inTransaction) { $this->dbConnection->rollBack(); } if ($e->getCode() != 40001 || $raceI === 10) { // pouzivam serialize izolaci, proto vice pokusu throw $e; } } }Konkrétní problém je ten, že očividně proběhne někdy více rollbacků než je otevřených transakcí. Co mám na tomto kódu špatně? Jedině snad, že by beginTransaction ve skutečnosti někdy tiše selhala, ale to snad není ani možné ne?
SQLSTATE[40001]: Serialization failure: 7 ERROR: could not serialize access due to read/write dependencies among transactions DETAIL: Reason code: Canceled on identification as a pivot, during commit attempt.Poradí mi někdo zkušený, zda je zaručeno, že commit zkončený chybou vždy způsobí ukončení transakce resp. daného SAVEPOINT?
Use COMMIT or ROLLBACK to terminate a transaction block.
Tiskni
Sdílej:
ISSN 1214-1267, (c) 1999-2007 Stickfish s.r.o.