Restarting transaction in MySQL after deadlock

空扰寡人 提交于 2020-01-11 08:24:39

问题


I am facing a deadlock in my MySQL. How can I configure MySQL to restart the transaction automatically when it encounters a deadlock?


回答1:


You cannot do that automatically, as the deadlock occurred, because two transactions were trying to modify the same data. If you know that simply retrying the same statements again will be the right thing to do, you need to implement this in your application.

However having the database do that automatically would be irresponsible, because there might just as well be cases where your application first needs to take a look at the new situation in the database, before issuing potentially modified statements, if any at all.




回答2:


Normally, you must write your applications so that they are always prepared to re-issue a transaction if it gets rolled back because of a deadlock.

http://dev.mysql.com/doc/refman/5.1/en/innodb-deadlocks.html

The manual suggests that no such configuration option exists.




回答3:


Restarting a transaction means:

  • (Optionally) initiate a new database connection handle;
  • Execute the first function/line code which initiates a new transaction and retries the entire execution path until commit.

Because the database engine cannot possibly know the queries which would have been executed after the deadlock occured, it cannot retry the whole thing for you (not to mention there could be application logic which would maybe execute different queries based on newly changed database data).




回答4:


Well, you can't and it would not make sense. It's up to your application to cope with deadlock and how it's done depends very much on your business logic, e.g. catch the exception, wait some seconds, retry x times ...



来源:https://stackoverflow.com/questions/6925769/restarting-transaction-in-mysql-after-deadlock

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