Skip to content

Commit

Permalink
silent rollback for nested transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
weisswurstkanone committed Dec 6, 2019
1 parent 41ab240 commit 057b2d5
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/SegmentAssignment/SegmentAssigner/SegmentAssigner.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,14 +187,20 @@ public function assignById(string $elementId, string $type, bool $breaksInherita
*/
public function removeElementById(string $elementId, string $type): bool
{
$db = $this->getDb();
$tActive = $db->isTransactionActive();

try {
$deletePattern = 'DELETE FROM %s WHERE `elementId` = :elementId AND `elementType` = :elementType; ';

$statement = sprintf($deletePattern, $this->getSegmentAssignmentTable()) .
sprintf($deletePattern, $this->getSegmentAssignmentQueueTable()) .
sprintf($deletePattern, $this->getSegmentAssignmentIndexTable());

$this->getDb()->beginTransaction();
if (!$tActive) {
// start a new transaction
$db->beginTransaction();
}

$this->getDb()->executeQuery($statement,
[
Expand All @@ -203,10 +209,15 @@ public function removeElementById(string $elementId, string $type): bool
]
);

$this->getDb()->commit();
if (!$tActive) {
$db->commit();
}

return true;
} catch (\Throwable $exception) {
if (!$tActive) {
$db->rollBack();
}
Logger::error($exception->getMessage());

return false;
Expand Down

0 comments on commit 057b2d5

Please sign in to comment.