If a PHP PDO transaction fails, should I roll back () explicitly?

I saw an example code in which someone does

$dbh->rollback();

when a PDOException occurs. I thought the database would automatically roll back in that case?

+5
source share
1 answer

If you are commitnot rollbackan open transaction, and not commitedsomewhere later in the script, it will not commited(as seen from the database engine) and will be automatically rolled back at the end of your script.


However, I (well, almost) always commitor rollbackexplicitly open transactions, therefore:

  • There is no risk of error (for example, committing "by mistake" later in the script)
  • /: $db->rollback(), , , , " , - ? script?"


"" PDOException: PHP , :

  • - , - , ""
+9

All Articles