B
B
BonBon Slick2019-02-22 12:41:04
Doctrine ORM
BonBon Slick, 2019-02-22 12:41:04

How to put everything in one DB transaction?

There is a controller that receives data via HTTP in the method.
Example

public function __construct($service_1, $service_2, $commandBus )
    {
    // bind parameters
       ...
    }

    public function method(Request $request)
    {
       $service_1->notifyAdmins(); // persist / flush, saves some data to db
$service_1->notifyCustomerByEmail(); // persist, flush  
$commandBus->dispatch(SaveProductToDb $command); // persist, flush
        return $data;
    }

The problem is that different services perform updates and writes to the database.
Let's say if services 1 and 2 were successful, and something fell off in the command, let's say the entity was not created, then the action crashes.
Only service 1 and service 2 have already made entries.
Let's imagine that service 1 makes an entry for admins, the user made an order, service 2 sends an email and a notification on the site, while the team creates and saves the order in the database.
As you can see, the user received a notification that the order was created, but the order will not be, because there was an error.
How to combine different services that change the database in one transaction?
It should be borne in mind that sometimes there may be some kind of API, for example, Google, to update the statistics of orders or payments, or something else.
I would like to be sure that all stages were successful.
So far, only put in order in my mind, that is, the command will go first if the order is not created, an error,
then series 2, send an email, if not sent - an error, and only then notify the admins, that is, service 1.
After all, even if you wrap into a tri-catch and register a transaction, a flush in the database will occur in the service, and part of the process will be recorded, which is undesirable.
It is necessary to do flush unit of work only once in the controller method, or somehow handle services and possible actions, or something else.
Thanks in advance for the ideas.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alex Wells, 2019-02-22
@Alex_Wells

MB I don’t understand something, but why is this not suitable?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question