A
A
Alex Wells2017-07-06 15:34:06
MySQL
Alex Wells, 2017-07-06 15:34:06

Laravel start transaction and end in different places?

Hello! There are two services, ServiceOne and ServiceTwo. Both are singletons.
From the controller, I pull $injectedServiceTwo->someMethod([1, 2, 3, 4]), in which I need to do some actions and reach the transaction. At the same time, it is important that one of the transaction requests be in ServiceOne, since it is he who is responsible for that request.
How to implement it correctly?
There is an option with DB::beginTransaction(), DB::commit() - but this will add a bunch of extra try catch, and somehow it's not right.
Can there be something better than this? Thanks in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
D3lphi, 2017-07-06
@D3lphi

Maybe this way will work for you:

DB::transaction(function () {
    // do somethimg...
    // Если внутри функции будет брошено исключение, то произойдет автоматический вызов DB::rollback().
    // Если же все пройдет "как надо", будет произведен автоматический коммит.
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question