duplicate key error does not cancel/rollback mysql transaction

情到浓时终转凉″ 提交于 2019-12-03 02:59:18
janon

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;

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!