mysql - INSERT 的 MySQL 事务是否锁定外键引用表?

标签 mysql transactions innodb isolation-level

我正在尝试在我的 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/

相关文章:

mysql - SQL : will SERIABLIZABLE insert visable to other non-serizable transaction

mysql - 关于 INNODB 锁定的困惑

php - Mysql 表插入性能

mysql - 是否可以在 mysql 中使用 "Index"作为列名

html - 根据组合框选择查询数据库

mysql - 有效地更新 mySQL 表?

java - Spring TransactionRequiredException 异常

c# - 如何回滚 Entity Framework 中的事务

php - 使用数据列填充下拉列表并获取用户选择的项目

mysql - 由 PAGE_CHECKSUM 引起的错误 1064 (42000)