-
Notifications
You must be signed in to change notification settings - Fork 22
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
Possible [Bug] when handling transactions #86
Comments
Hi @dijitaltrix -- I am unable to reproduce the error you describe. I tried doing so in the issue-86 branch, via this commit: 0c7cd7e Take a look at that test, and let me know how to make it match your case more closely. |
Hi @pmjones, Thanks for taking a look, it may be the issue is with MariaDB or the PDO driver. I've run the tests from Issue-86 and can confirm they test ok against an sqlite memory database, I've not modified the test you supplied as the issue only seems to occur using MySQL and though I modified the phpunit tests in orm/tests they threw a few errors due to backticks and I wasn't confident of the results. I've created a repository here https://github.com/dijitaltrix/Issue-86 with the minimum code required to show the issue. It follows the examples in the docs and the code in Issue86 very closely. You'll see two files mysql_create.php and sqlite_create.php they are identical apart from the new AtlasContainer() statement. The Sqlite code runs as excepted, it creates the parent and relations as it should, and when an Exception is thrown no records are created. With MySQL the same code gives this error:
|
Hi @dijitaltrix I looked at your tests and the error is because you are trying to use And that don't work so I change to A better question might be why PDO don't report the error from mysql |
Hi @sunkan |
Hi @pmjones There's four test files now.. I assumed the issue was only occurring in MySQL but it also occurs in Sqlite, they're almost identical as before so take your pick! I think the issue may be to do with the response from an Aura SQL call $this->pdo->inTransaction() it seems to return 1 on the first transaction but not on subsequent transactions. |
The problem is in Atlas\Orm\Table\ConnectionManager::getConnection It checks if the connection already exists and just returns it. It should also check inTransaction is true like this if (isset($this->conn[$type][$tableClass])) {
if ($this->inTransaction() && ! $this->conn[$type][$tableClass]->inTransaction()) {
$this->conn[$type][$tableClass]->beginTransaction();
}
return $this->conn[$type][$tableClass];
} I will put together a pull request in a an hour need to do some actual work now :) |
@sunkan I look forward to it, and thanks! |
When getting a cached connecting do a check to see if we should begin a transaction or not
@dijitaltrix I have merged the fix from @sunkan into the 2.x branch, with a refactoring of the logic therein. Try it out and let me know if it still works for you; if it does, I'll make a release. Thanks, everyone, for your work here. |
Closing this as "bug has been fixed" and will wait for separate PR from @dijitaltrix for a test. Thanks everyone! |
I've just started with Atlas so forgive me if I'm missing something but I have a problem with transactions, I don't think they are working as intended.
I have a simple example, an Invoice with many child Items which I want to save inside a transaction. This is happening when I'm creating dummy invoices in a loop, the data is generated with Faker, the first create fails as expected, no items and no invoice but the subsequent invoices are created when the invoice items are not.
$invoice = $invoice_repository->create($data);
Here is some example input:
This data is passed to the create function in InvoiceRepository.php, this code works fine but may not be optimal as I couldn't find a way to get persist() to save the invoice items directly from the array, they had to be converted to a recordSet:
However once I added an event which threw an Exception I noticed the 'invoice' was created even though the 'invoice items' weren't created and the Exception was raised showing the "The Transaction failed" message.
Unless I have the code in the InvoiceRepository create function wrong I'd expect both the 'invoice' and 'invoice items' not to be created.
Here's my versions:
The text was updated successfully, but these errors were encountered: