How to handle PDO transactions with possible duplicate entries
What happens if I commit a pdo transaction where one or more entries are duplicates 开发者_开发问答of already existing primary keys? Will the transaction insert all of the none duplicates or roll back?
If it is the latter, how can I prevent the rollback?
You won't be able to commit such transaction, because you won't be able to insert such rows in the first place.
See below In MySQL console #1 we do this:
mysql> USE TEST;
Database changed
mysql> CREATE TABLE trTest(ID INT UNSIGNED PRIMARY KEY) ENGINE = InnoDB;
Query OK, 0 rows affected (0.46 sec)
mysql> START TRANSACTION;
Query OK, 0 rows affected (0.00 sec)
mysql> INSERT INTO trTest VALUES (1);
Query OK, 1 row affected (0.00 sec)
Then in console #2 we try:
mysql> USE TEST;
Database changed
mysql> START TRANSACTION;
Query OK, 0 rows affected (0.00 sec)
mysql> INSERT INTO trTest VALUES (1);
The prompt will not return here and the session will hang for a while until...
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
精彩评论