Skip to content

Magento resource model can not rollback transactions if error happend #34066

@RomanKrut

Description

@RomanKrut

We are using magento 2.3 instance on production and we have faced issue with transactions while saving some model to database. But when error happens we can`t log that to database because of transaction level.

Examples (*)

Lets look at https://github.com/magento/magento2/blob/2.4-develop/lib/internal/Magento/Framework/Model/ResourceModel/Db/AbstractDb.php save function
At line 402 we have beginTransaction method. It sets transaction level from 0 to 1 level.
If on the next code some Error happend ( any of this https://www.php.net/manual/en/class.typeerror.php)
It is important that we are faced not an Exception, we are faced an Error object
And catch at line 432 - catchs only Exceptions
It means that transactions will never be rollbacked and the next pieces of code will save nothing to database because of that if we will use same connection instance.

Steps to reproduce (*)

  1. Write declare(strict_types=1) at the beginning of file https://github.com/magento/magento2/blob/2.4-develop/lib/internal/Magento/Framework/Model/ResourceModel/Db/AbstractDb.php - this step is only for reproduce
  2. After $this->beginTransaction(); in save method write preg_match('/\d{10}/', 123); - this step is only for reproduce
  3. Save some entity programmatically or any other way.
  4. Debug this code and see that when preg_match will be executed, we will not catch that error and we will not rollback transaction here https://i.imgur.com/36hC27c.png

Proposed solution

I propose replace catch (\Exception $e) with catch (\Throwable $e).

Please provide Severity assessment for the Issue as Reporter. This information will help during Confirmation and Issue triage processes.

  • Severity: S0 - Affects critical data or functionality and leaves users with no workaround.
  • Severity: S1 - Affects critical data or functionality and forces users to employ a workaround.
  • Severity: S2 - Affects non-critical data or functionality and forces users to employ a workaround.
  • Severity: S3 - Affects non-critical data or functionality and does not force users to employ a workaround.
  • Severity: S4 - Affects aesthetics, professional look and feel, “quality” or “usability”.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area: FrameworkComponent: ExceptionIssue: ConfirmedGate 3 Passed. Manual verification of the issue completed. Issue is confirmedPriority: P3May be fixed according to the position in the backlog.Progress: ready for devReproduced on 2.4.xThe issue has been reproduced on latest 2.4-develop branchTriage: Dev.ExperienceIssue related to Developer Experience and needs help with Triage to Confirm or Reject it

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions