본문 바로가기
기타/PHP

There is no active transaction

by 죠부니 2022. 10. 26.
반응형

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:

  1. Get the Connection.
  2. Start the Transaction with $connection->beginTransaction()
  3. 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.

 

반응형

'기타 > PHP' 카테고리의 다른 글

cURL error 60: SSL certificate  (0) 2022.11.25
'' // "" // 따옴표처리  (0) 2020.03.31
fcm  (0) 2019.09.10
curl  (0) 2019.09.10
implode,explode  (0) 2019.06.20