Skip to content

feat: Add a callback to be called on transaction failure #55338

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

Merged

Conversation

dshafik
Copy link
Contributor

@dshafik dshafik commented Apr 9, 2025

This PR adds a new argument to DB::transaction() that allows you to pass in a callback to be executed when the transaction fails:

DB::transaction(function () {
 // do DB stuff
}, onFailureCallback: function () {
    Notification::send($admin, new SomethingImportantBroke());
});

@dshafik dshafik force-pushed the add-on-failure-callback-transactions branch from b668a42 to 65fcd2d Compare April 9, 2025 11:40
@dshafik dshafik force-pushed the add-on-failure-callback-transactions branch from 65fcd2d to cb518a9 Compare April 9, 2025 11:44
@taylorotwell taylorotwell merged commit 7d65ce0 into laravel:12.x Apr 9, 2025
47 of 48 checks passed
@taylorotwell
Copy link
Member

Thanks 👍

@taylorotwell
Copy link
Member

I need to revert this one since it's a breaking change on that interface. Sorry!

@negoziator
Copy link

negoziator commented Apr 16, 2025

Thank you @taylorotwell ! I was just about to comment on it, because of e.g this❤️

@dshafik
Copy link
Contributor Author

dshafik commented Apr 16, 2025

I was going to do that today also, sorry about that! I didn't consider there were outside consumers of the interface, my bad! I'll be more careful in future.

@negoziator
Copy link

I was going to do that today also, sorry about that! I didn't consider there were outside consumers of the interface, my bad! I'll be more careful in future.

I think it was very good thinking even though 😀

@rodrigopedra
Copy link
Contributor

@dshafik please consider sending it to master, it is indeed a nice contribution

@negoziator
Copy link

@dshafik I was thinking.

Maybe it could be possible to implement a new function e.g transactionWithCallback() 🤔

I didn't think it through quite yet - but i really like the idea of onFailure() callback in the trasaction

@alcaeus
Copy link
Contributor

alcaeus commented Apr 17, 2025

When looking at the feature in more detail trying to replicate it for the MongoDB integration (which has slightly different transaction handling due to how transactions work in MongoDB), I wasn't quite sure why the callback was preferable to a try-catch block:

try {
    DB::transaction(function () {
        // TODO: World peace
    });
} catch (Throwable $e) {
    // TODO: Save the world
}

I'm sure I'm missing some part of this, maybe @dshafik could help me understand why the above isn't sufficient?

@moe-mizrak
Copy link

I created a lightweight package which can be extended (since it is chainable instead of handling it with params):

Basically it wraps DB::transaction() so that we can use it as

$result = TransactionBuilder::make()
    ->attempts(3) // number of attempts
    ->run(function () {
        // your transaction logic
        return 'test';
    })
    ->onFailure(function ($exception) {
        // your onFailure callback logic
        logger()->error($exception->getMessage());
    })
    ->result();

https://github.com/moe-mizrak/transaction-builder

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants