laravel / octane

Supercharge your Laravel application's performance.
https://laravel.com/docs/octane
MIT License
3.71k stars 284 forks source link

Rollback existing transactions after request ended #912

Closed vatsake closed 3 weeks ago

vatsake commented 3 weeks ago

When the script ends or when a connection is about to be closed, if you have an outstanding transaction, PDO will automatically roll it back. This is a safety measure to help avoid inconsistency in the cases where the script terminates unexpectedly--if you didn't explicitly commit the transaction, then it is assumed that something went awry, so the rollback is performed for the safety of your data.

PHP PDO transactions

Since Octane never actually quits, the transaction will still exist after the request has ended.

Consider the following code:

DB::beginTransaction();
User::create(['name' => $request->input('name')]);
if (someCondition()) {
   abort(403);
)
DB::commit();

If for some reason someCondition() is true, the request is aborted, but the transaction is not ended. If the user tries again and someCondition() is false, then both the new and previous 'User' model is created.

Fixes

909

Unfortunately I don't know how to test this. Hopefully someone will add it.

taylorotwell commented 3 weeks ago

I would add this to your own application if you need it.

driesvints commented 3 weeks ago

Sorry @vatsake. Seems like we're not going to make this change in core atm.