-
Notifications
You must be signed in to change notification settings - Fork 11.3k
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
PDOException: SQLSTATE[42000]: Syntax error or access violation: 1305 SAVEPOINT trans2 does not exist #18429
Comments
Facing the same error on app("db")->transaction(function () use (
$company,
$company_detail,
$user
) {
$company = $this->company->create($company,
$company_detail);
$user['company_id'] = $company->id;
app('Illuminate\Contracts\Console\Kernel')->call('tenant:new',
['id' => $company->id]);
$this->users->create($user);
}); |
I assume your yest class uses |
@mrliptontea , |
So what's happening here is Laravel is trying to simulate nested transactions using savepoints but, from my experience, they don't quite work like that. Not sure what best practices are in such cases but it is not framework's error. One option, and that's something which I have done in the past to test code that was using transactions, is to remove |
This was working just fine in 5.3, somehow the transactions stopped working for a custom database in 5.4 :-/ |
Experiencing the same issue. Is there a way this could somehow be supported? |
@mrliptontea I have been using nested transactions for quite some time and they've worked successfully. I can confirm they are working on my local dev box. I start getting the same error messages only when the server load is high. Laravel 5.3.31 |
From the documentation:
Eloquent is trying to mitigate this limitation by using savepoints, which are only available in InnoDB. It's hard for me to say how savepoints are different from nested transactions, but they do behave differently. I also experienced unexpected 'savepoint does not exist' errors in the past, using different ORMs, hence I try to avoid nesting and don't think there's anything that can be done to Laravel's code to fix this. |
@mrliptontea Thank you for your quick response! I read that in the documentation but it didn't make sense to me since I have been using nested transactions successfully for so long. I thought Eloquent was using some kind of method to solve this, as I also do not have any implicit commit issues when calling What I can gather from this thread, and correct me if I'm wrong, is that nested transactions should be avoided as there isn't any proper solution at the moment. Even though Eloquent uses savepoints to simulate nested transactions there are still issues that arise periodically. |
That's basically what I meant. |
I have ran into this problem on MySQL as well and it's really thrown a wrench in trying to write Unit tests.. It is also not a consistent problem. I use RefreshDatabase in my tests with MySQL as the backend (so it creates a transaction right off the bat).. In the course of my tests, I have found that calls to DB::transaction() sometimes fail and sometimes succeed depending on the actions taken WITHIN the transaction. Specifically it seemed to be related to if I was saving relationships instead of top-level models: I.e.
If This feels like a bug, I think it should be reopened and investigated a bit further... |
I have this problem too after manually upgrading from Laravel |
Maybe it's helpful to somebody. I found out what caused the issue for me when running my tests. I've written an api-to-database importer and I'm using |
I also have the same problem if I use |
@pixelpeter @oceanapplications I recommend to create a new issue and link it with this one. Continue conversation in issues closed long time ago isn't productive. |
I have the same issue and scenario (AWS Aurora instance). Did you find a way to fix it? |
I know the issue is closed but for anyone looking for a solution. I had a similar problem and the following resolved it for me.
When an exception got thrown within any db operation, it got rolled back to the previous state and any committed db ops was discarded. |
I got this issue too, but it was because I forgot to run |
I was experiencing this problem as well. My migrations worked fine when run manually, but when running the test suite I got the same error as the OP. I was trying to do a complex migration and couldn't do that using the In the end I solved it by swapping out a I hope this helps someone. |
In my case, it works in my local environment, but it fails in CI. |
When having the same issue, explicitly committing the transaction worked for me: try {
DB::beginTransaction();
//do stuff
DB::commit();
//return something
} catch (\Exception $e) {
DB::rollback();
//do something else
} |
@ribeiroplinio I'm also having the same issue where I get this error For your reference here is my code screenshot |
I solve with docker, this my docker-file.com, a old project in laravel.
|
I recently had this issue when running tests on Laravel 10; PHP 8.3, and in my case it was caused by using I solved it by replacing |
@EricMcWinNer What about the id? With delete you loose resetting the auto increment. I have the same problem within a Laravel Nova action now. I guess that Nova does some implicit wrapping in |
Description:
Error when runninng phpunit tests:
Steps To Reproduce:
Create some service class which use transaction.
Put code something like this:
then use it from controller:
then make test for it like this:
finally execute test:
The text was updated successfully, but these errors were encountered: