Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

duplicate key error does not cancel/rollback mysql transaction

When in a mysql innodb transaction, I would expect a duplicate key error to cause a rollback. It doesn't, instead it simply throws an error and continues on to the next command. Once the COMMIT command is reached, the transaction will be committed, sans the duplicate key causing command.

Is this the expected behaviour? If so, how would one go about setting it up so that the transaction is rolled back instead of committed when such an error occurs?

test environment:

CREATE TABLE `test` (  
  `id` int(11) NOT NULL, 
  PRIMARY KEY (`id`) 
) ENGINE=InnoDB DEFAULT CHARSET=latin1 

BEGIN;
     INSERT INTO test VALUES (5);
     INSERT INTO test VALUES (5);
COMMIT;

expected result: table test is empty

actual result: table test contains one record with value 5

like image 412
allixsenos Avatar asked Oct 27 '09 12:10

allixsenos


People also ask

What is a rollback in MySQL?

In a set of operations, if one of them fails, the rollback occurs to restore the database to its original state. If no error occurs, the entire set of statements is committed to the database. MySQL provides us with the following important statement to control transactions:

How do I rollback or cancel the current transaction?

To roll back the current transaction and cancel its changes, you use the ROLLBACK statement. To disable or enable the auto-commit mode for the current transaction, you use the SET autocommit statement.

How do I control transactions in MySQL?

MySQL provides us with the following important statement to control transactions: To start a transaction, you use the START TRANSACTION statement. To commit the current transaction and make its changes permanent, you use the COMMIT statement. To roll back the current transaction and cancel its changes, you use the ROLLBACK statement.

How to recover deleted rows from the Orders table in MySQL?

As you can see from the output, MySQL confirmed that all the rows from the orders table were deleted. Second, log in to the MySQL database server in a separate session and query data from the orders table: In this second session, we still can see the data from the orders table.


2 Answers

If an insert fails because of a duplicate, the database rolls the transaction back to the start of that statement.

It uses an internal savepoint made at the beginning of the statement, then rolls back to that savepoint.

It does NOT roll back the entire transaction, because that might not have been what you wanted.

The behaviour of the mysql client is configurable using command line parameters. It can either quit (which would implicitly rollback) or continue.

If you're using your own app, what it does is up to you.


Mysql does not impose POLICY on how you handle failures - it leaves that up to your application. So what you do about them is your own business - you can ignore them if you like.

like image 69
MarkR Avatar answered Nov 09 '22 05:11

MarkR


MySql (and other sql engine AFAIK) does not automatically rollback a transaction if an error occurs.

You have to declare an error handler that will Rollback the transaction:

DECLARE EXIT HANDLER FOR SQLEXCEPTION, SQLWARNING, NOT FOUND
BEGIN 
  ROLLBACK; 
  CALL ERROR_ROLLBACK_OCCURRED; 
END;
like image 37
janon Avatar answered Nov 09 '22 05:11

janon