There is no active transaction
https://stackoverflow.com/questions/41392982/php-pdo-there-is-no-active-transaction
PHP PDO - There is no active transaction
I am having problem with transactions in php script. I would like to make multiply queries and be able to recall them all, if at least one of them fails. Below you can find a simple example of the ...
stackoverflow.com
한줄요약 beginTransaction()의 경우 try catch문 밖에 위치해야한다.
==
The $connection->beginTransaction() must be outside of the try-catch block. When you're start the beginTransaction() in the try-block and your Database Operations throws an exception, the catch-block doesn't know something from an active transaction. So, you get the same error:
"There is no active transaction".
So the structure should be as well:
- Get the Connection.
- Start the Transaction with $connection->beginTransaction()
- Open the try-catch block.
The try-block contains the $connection->commit() after DB Operations.
The catch-block contains the $connection->rollback() before a throw Exception.