A single action in PHP has to go through the following steps:
- Execute a no. of mysql statements from PHP PDO, based on lots of business logic.
- Execute a stored procedure.
- Perform some more MySQL statements from PDO.
The entire process needs to be a single transaction. If any error takes place in the MySQL stored procedure, the entire transaction has to be rolled back. (The stored procedure has queries to create temporary tables, do a cursor-based scan and perform inserts.) Even if the error occurs in the PDO after the stored procedure, the transaction has to be completely rolled back, including any changes that occurred in the stored procedure.
The PDO based queries were programmed quite sometime back. And the stored procedure is being newly introduced based on new requirements from client.
In PHP, a Transaction is started at the start of Step 1. And there is a commit at the end of Step 3. And there is a catch block at the end, which causes rollback.
Should a Transaction be started in the stored procedure? If so, how to rollback completely on error? Or should we manually set auto-commit to false in the stored procedure? Or, is there some other way to inform MySQL that this stored procedure is already a part of a transaction.
If not, is the atomicity guaranteed for the entire action?