New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Magento resource model can not rollback transactions if error happend #34066
Comments
Hi @krutkoroma79911. Thank you for your report.
Please make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, please, add a comment to the issue:
For more details, please, review the Magento Contributor Assistant documentation. Please, add a comment to assign the issue:
🕙 You can find the schedule on the Magento Community Calendar page. 📞 The triage of issues happens in the queue order. If you want to speed up the delivery of your contribution, please join the Community Contributions Triage session to discuss the appropriate ticket. 🎥 You can find the recording of the previous Community Contributions Triage on the Magento Youtube Channel ✏️ Feel free to post questions/proposals/feedback related to the Community Contributions Triage process to the corresponding Slack Channel |
Hi @krutkoroma79911. I agree with you. As far as M2 moved to PHP 7+ it would be very good to replace all catch(\Exception){} with catch(\Throwable){}, as far as from PHP 7 \Throwable become the basic interface for catching exceptions/errors. |
Hi @engcom-Lima. Thank you for working on this issue.
|
Hi @krutkoroma79911, Can you please elaborate on the errors that you are getting while dealing with db transactions ? It would be helpful for further analysis if we can replicate the same errors at our end. Thanks |
Hi @engcom-Lima . It happened with our customization of order address model, not with magento core functionality.
|
declare(strict_types=1);
....
public function save(\Magento\Framework\Model\AbstractModel $object)
{
if ($object->isDeleted()) {
return $this->delete($object);
}
$this->beginTransaction();
try {
preg_match('/\d{10}/', 123);
if (!$this->isModified($object)) {
$this->processNotModifiedSave($object);
$this->commit();
$object->setHasDataChanges(false);
return $this;
}
$object->validateBeforeSave();
$object->beforeSave();
if ($object->isSaveAllowed()) {
$this->_serializeFields($object);
$this->_beforeSave($object);
$this->_checkUnique($object);
$this->objectRelationProcessor->validateDataIntegrity($this->getMainTable(), $object->getData());
if ($this->isObjectNotNew($object)) {
$this->updateObject($object);
} else {
$this->saveNewObject($object);
}
$this->unserializeFields($object);
$this->processAfterSaves($object);
}
$this->addCommitCallback([$object, 'afterCommitCallback'])->commit();
$object->setHasDataChanges(false);
} catch (DuplicateException $e) {
$this->rollBack();
$object->setHasDataChanges(true);
throw new AlreadyExistsException(new Phrase('Unique constraint violation found'), $e);
} catch (\Exception $e) {
$this->rollBack();
$object->setHasDataChanges(true);
throw $e;
}
return $this;
} and call model save() for example product, category, etc. |
Hi @krutkoroma79911, @ameysar, Thank you for the reply. As you said, this error is from the customised code's side which can be handles there itself and not from the Magento Implementation. I asked for the errors you are getting so that I can understand the situation better. What I think is, If we replace Hope this clarifies. |
@engcom-Lima |
@engcom-Lima |
Hi @krutkoroma79911 , @ameysar, Thank you guys for your inputs. I did some research on the same and found that it might not be bad if we can implement Throwable instead of Exception since it covers all the errors as well including TypeError. I am confirming this not as a bug but on the basis that we'll analyse it further and take action accordingly. Thanks |
✅ Jira issue https://jira.corp.magento.com/browse/AC-1396 is successfully created for this GitHub issue. |
✅ Confirmed by @engcom-Lima. Thank you for verifying the issue. |
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 (*)
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.
The text was updated successfully, but these errors were encountered: