2

I have found the code that uses database transaction (\Magento\SalesRule\Model\ResourceModel\Rule::saveStoreLabels):

public function saveStoreLabels($ruleId, $labels)
{
    ...
    $connection = $this->getConnection();
    ...
    $connection->beginTransaction();
    try {
        ...
    } catch (\Exception $e) {
        $connection->rollback();
        throw $e;
    }
    $connection->commit();

    return $this;
}

I want to get nested transactions where inner transaction should be processed silently (without exception) and outer transaction should be committed if no error occured (even inner transaction is rolled back):

public function someOtherMethod(...)
{
    ...
    $connection = $this->getConnection();
    ...
    $connection->beginTransaction();
    try {
        ...
        /* nested transaction should be committed or rolled back without exception */
        $connection->beginTransaction();
        try {
            ...
            $connection->commit();
        } catch (\Exception $e) {
            $connection->rollback();
        }

    } catch (\Exception $e) {
        $connection->rollback();
        throw $e;
    }
    /* outer transaction should be committed if exception was not thrown */
    $connection->commit();

    return $this;
}

Is it possible?

1 Answer 1

1

No, Mysql does not support nesting transaction.

You can emulate this behavior by:

  • using savepoint

  • using different connections for inner and outer operation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.