Home  >  Article  >  Backend Development  >  In PHP, after starting a transaction, the program throws an exception and neither commit nor rollback is executed. Will the MySQL transaction be rolled back?

In PHP, after starting a transaction, the program throws an exception and neither commit nor rollback is executed. Will the MySQL transaction be rolled back?

WBOY
WBOYOriginal
2016-08-10 09:07:172181browse

Some people on the Internet say that after opening a transaction, the SQL statement execution error or program error occurs. If rollback is not executed, the next run will automatically commit
Program error will not rollback rollback

But some people say that exceptions are thrown. If MySQL has an uncommitted transaction, then the AUTOCOMMIT automatically submitted parameter should be 0 at this time.
However, if you operate through MySQL drivers in other languages, these drivers generally have the function of automatically recovering AUTOCOMMIT. After the request processing is completed, unprocessed transactions will be automatically ROLLBACKed.

Reply content:

Some people on the Internet say that after opening a transaction, the SQL statement execution error or program error occurs. If rollback is not executed, the next run will automatically commit
Program error will not rollback rollback

But some people say that exceptions are thrown. If MySQL has uncommitted transactions, the AUTOCOMMIT automatically submitted parameter should be 0 at this time.
However, if you operate through MySQL drivers in other languages, these drivers generally have the function of automatically recovering AUTOCOMMIT. After the request processing is completed, unprocessed transactions will be automatically ROLLBACKed.

Here I assume by default that you are using PDO.
The official document says this:

When the script ends or when a connection is about to be closed, if you have an outstanding transaction, PDO will automatically roll it back. This is a safety measure to help avoid inconsistency in the cases where the script terminates unexpectedly--if you didn't explicitly commit the transaction, then it is assumed that something went awry, so the rollback is performed for the safety of your data.
When the script ends or when a connection is about to be closed

It means that when the script ends (including normal end or abnormal end).

PDO will automatically roll it back

PDO will automatically roll back.
The document has made it very clear.
If you are not using the PDO driver, you can check the relevant driver documentation, or state clearly what driver you are using in the question.

By the way, the previous mysql_connect() function is officially no longer recommended (was deprecated).

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn