我正在尝试在我的 Java 应用程序中进行大量事务,并为 user_account_entry
表执行单个插入条目(以千为单位),该表具有对 user
表的外键引用.
当事务运行时,我无法更新属于获取 LockAcquisitionException
我正在使用 MySQL InnoDB 并为事务使用 DEFAULT
隔离级别,转换为 InnoDB 的 REPEATABLE-READ
级别,任何人都可以阐明 mysql 期间的外键锁定交易
最佳答案
是的。
演示:在一个窗口中,创建父表和子表。
mysql1> create table parent (id int primary key, x int );
Query OK, 0 rows affected (0.04 sec)
mysql1> create table child (id int primary key, parentid int,
foreign key(parentid) references parent(id));
Query OK, 0 rows affected (0.03 sec)
往父表中插入一行:
mysql1> insert into parent values (1, 1);
Query OK, 1 row affected (0.02 sec)
启动事务并向子表添加一行,引用父行:
mysql1> begin;
Query OK, 0 rows affected (0.00 sec)
mysql1> insert into child values (42, 1);
Query OK, 1 row affected (0.00 sec)
打开第二个窗口,并尝试更新父级中引用的行:
mysql2> update parent set x = 2 where x = 1;
它挂起,等待第一个 session 持有的锁。
回到第一个窗口并提交事务,这会释放锁:
mysql1> commit;
Query OK, 0 rows affected (0.02 sec)
在第二个窗口中,更新继续进行,计时显示它等待了将近 6 秒,这是我回到第一个窗口进行提交所用的时间。
Query OK, 1 row affected (5.92 sec)
Rows matched: 1 Changed: 1 Warnings: 0
关于mysql - INSERT 的 MySQL 事务是否锁定外键引用表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54672633/