I’ve seen an code example where someone does a
$dbh->rollback();
when there occurs an PDOException. I thought the database will rollback automatically in such a case?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If you don’t
commitnotrollbackan opened transaction, and it’s notcommitedanywhere later in your script, it won’t becommited(as seen by the database engine), and will automatically rolled-back at the end of your script.Still, I (well, almost) always
commitorrollbackexplicitly the transactions I open, so :$db->rollback(), he knows I want the transaction rolled-back for sure, and he doesn’t have to think “did he really want to rollback, or did he forget something ? and what about later in the script ?“The DB engine doesn’t “see” the PDOException : it is thrown by PHP under various conditions — but the database doesn’t rollback anything by itself :