After starting a transaction in PHP, the program throws an exception and does not execute commit or rollbackmysql. Will the transaction be rolled back?

Source: Internet
Author: User
Some people on the Internet say that the SQL statement execution error or program error does not execute rollback after the transaction is enabled, the next run will automatically commit program errors will not roll back rollback, but some people say that throwing exceptions and other programs if MySQL has not committed transactions, then AUTOCOM... some people on the Internet say that after the transaction is enabled, if the SQL statement execution error or program error does not execute rollback, the next operation will automatically commit
Rollback is not rolled back when a program error occurs.

However, it is also said that if a program such as throwing an exception has a transaction not committed by MySQL, the parameter automatically submitted by AUTOCOMMIT should be 0 at this time.
However, if you use the MySQL driver in other languages, these drivers generally provide the AUTOCOMMIT recovery function. after the request is processed, the transactions that are not processed are automatically rolled back.

Reply content:

Some people on the Internet say that after the transaction is enabled, if the SQL statement execution error or program error does not execute rollback, the next operation will automatically commit
Rollback is not rolled back when a program error occurs.

However, it is also said that if a program such as throwing an exception has a transaction not committed by MySQL, the parameter automatically submitted by AUTOCOMMIT should be 0 at this time.
However, if you use the MySQL driver in other languages, these drivers generally provide the AUTOCOMMIT recovery function. after the request is processed, the transactions that are not processed are automatically rolled back.

I think you are using PDO by default.
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 case 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 saved Med for the safety of your data.
When the script ends or when a connection is about to be closed

This means that when the script ends (including normal or abnormal termination ).

PDO will automatically roll it back

PDO will automatically roll back.
The document is already clear.
If you are not using the PDO driver, you can view the relevant driver documentation or clarify what driver is used in the question.

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.