数据库
首页 > 数据库> > MySQL:事务隔离级别,死锁

MySQL:事务隔离级别,死锁

作者:互联网

我的导入产品的过程很漫长,有时会出现死锁错误.据我所知,我认为如果在脚本执行过程中将隔离级别切换为“可序列化”,那么我将解决死锁问题.但是,我该怎么办,我可以打开2个终端并使用Serializable重现死锁.

conn1: SET GLOBAL TRANSACTION ISOLATION LEVEL SERIALAZIBLE;
conn1: START TRANSACTION;
conn2: START TRANSACTION;
conn1: UPDATE core_config_data set value = 1 WHERE config_id = 1;
conn2: UPDATE core_config_data set value = 1 WHERE config_id = 2;
conn1: UPDATE core_config_data set value = 1 WHERE config_id = 2; waiting...
conn2: UPDATE core_config_data set value = 1 WHERE config_id = 1; ERROR 1213 (40001): Deadlock found when trying to get lock; try restarting transaction

好的,在我尝试使用READ UNCOMMITED进行脏读时:

conn1: SET GLOBAL TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
conn1: START TRANSACTION;
conn1: SELECT junk FROM employees WHERE employee_id = 1; junk=test;
conn2: START TRANSACTION;
conn2: update employees set junk='test1' where employee_id = 1;
conn1: SELECT junk FROM employees WHERE employee_id = 1; junk=test;

你知道我弄错了吗?

解决方法:

一切都如预期.您根本无法仅凭隔离级别来避免死锁.隔离级别仅用于确保数据的完整性.

您可以通过确保更新行的顺序相同来解决许多死锁问题.这样,第一个连接将能够处理到最后而不会出现死锁,然后连接2也将失败或成功.

标签:isolation-level,transactions,database-deadlocks,mysql
来源: https://codeday.me/bug/20191121/2052806.html