How to complete the transaction PDO operation

Due

Inadvertently look APP geek college, ready to find some tutorials to see. See a PDO safe handling and transaction processing , a want for MySQL transaction merely conceptual (know to perform multiple statements, one of which failed , it will roll back operation). But the concept into the code really did not practice too, so he opened.

Explain the basis of the video is quite detailed:

MySQL data tables must be InnoDB type

Transaction processing methods of operation include:

  1. beginTransaction () // start a transaction

  2. commit () // transaction commits

  3. rollBack () // transaction rollback operation

PS:

PDO :: ATTR_ERRMODE: Error Reporting

PDO :: ERRMODE_SILENT: Set only error code

PDO :: ERRMODE_WARNING: E_WARNING error caused

PDO :: ERRMODE_EXCEPTION: an exception is thrown exceptions

But which piece of code is problematic, first of all I see is roolback misspelled, then a closer look: the first die off, and then execute rollBack method, there is definitely a problem ah.

But the actual implementation process, because there is a unique index on the name field, the transaction is not successfully submitted. And rollBack method is not implemented. But the data is not inserted. I have been thinking all morning to get the problem, because of the emergence of the problem, but also lead to take several hours to solve.

problem

问题:Is it necessary to rollback if commit fails?

$mysqli->autocommit(false); //Start the transaction
$success = true;    

/* do a bunch of inserts here, which will be rolled back and
   set $success to false if they fail */

if ($success) {
    if ($mysqli->commit()) {
        /* display success message, possibly redirect to another page */
    }
    else {
        /* display error message */
        $mysqli->rollback(); //<----------- Do I need this?
    }
}

$mysqli->autocommit(true); //Turns autocommit back on (will be turned off again if needed)

//Keep running regardless, possibly executing more inserts

~ ~ ~ Also have the same question on SF and no one can answer.

solve

Then they find, it seems someone also told me the same question, rollBack method is performed automatically? Finally found the answer!

If you don't commit not rollback an opened transaction, and it's not commited anywhere later in your script, it won't be commited (as seen by the database engine), and will automatically rolled-back at the end of your script.

Cited above probably means: a thing is not performed in the open method commit or rollBack method, and finally commit method is not performed in the script and the final will be executed rollBack method in the script.

Reference also mentioned helps people understand, otherwise I do not know if I need a rollback operation, or after write.

The code is more easy to read / understand : when one sees $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 ?"

note

So, imagine a unique index field into the same data, causing the program to enter the captured exception. At this time no write rollback rollback operation. The project team others do not know the final execution performed rollback operation. A big write a rollback method, after a lot of code, small d wrote a rollback operation, which in turn may throw a new exception: "There is no active transaction", do not start a active transaction.

reference

http://www.jikexueyuan.com/course/653.html
http://stackoverflow.com/questions/38133260/is-it-necessary-to-rollback-if-commit-fails
http://stackoverflow.com/questions/2001698/if-an-php-pdo-transaction-fails-must-i-rollback-explicitely

Guess you like

Origin www.cnblogs.com/luyuqiang/p/pdo-transaction.html