laravel / octane

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

Rollback any existing transactions after request ended #909

Closed vatsake closed 3 weeks ago

vatsake commented 3 weeks ago

Consider the following code:

DB::beginTransaction();
// Lots of inserts
// Insert user
// Insert/add roles
if (someCondition()) {
   abort(403);
)
DB::commit();

If someCondition() is true the first time, then the transaction is not rolled back. If the second time someCondition() is false then the previous transaction's queries are also committed.

I used to code with this in mind:

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 But since Octane never actually quits, the transaction is not rolled back.

So my suggestion would be to add an event to a listener for example RequestTerminated that rolls back any existing transaction. So that unexpected rows aren't committed to the database.

driesvints commented 3 weeks ago

Hi there. I think you make a valid suggestion here. However, this isn't something we're going to be working on ourselves anytime soon. We'd appreciate either a PR to the docs or a PR to Octane to implement this. Thank you.