gpt4 book ai didi

带有引发触发器的插入的 MySQL 死锁

转载 作者:太空宇宙 更新时间:2023-11-03 10:39:43 25 4
gpt4 key购买 nike

我发现发生了一些罕见的死锁错误。我知道当两个查询作业相互依赖时会发生死锁,因此 MySQL 会回滚其中一个。但在我的情况下,MySQL 处于自动提交模式,我正在插入一条触发触发器的新记录。所以我不明白它产生死锁情况的原因。

这是我的表架构:

---- 用户表----

CREATE TABLE `users` (
`insta_id` bigint(20) unsigned NOT NULL,
`name` varchar(50) NOT NULL,
`password` varchar(60) NOT NULL,
`gem` int(10) unsigned DEFAULT '20',
`coin` int(10) unsigned DEFAULT '20',
PRIMARY KEY (`insta_id`),
UNIQUE KEY `insta_id` (`insta_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1

---like_requests表---

CREATE TABLE `like_requests` (
`req_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`insta_id` bigint(20) unsigned NOT NULL,
`media_id` varchar(50) NOT NULL,
`remaining_like` int(10) unsigned NOT NULL,
`active` tinyint(1) NOT NULL DEFAULT '1',
`count` int(10) unsigned NOT NULL,
PRIMARY KEY (`req_id`),
KEY `insta_id` (`insta_id`),
KEY `media_id` (`media_id`),
CONSTRAINT `like_requests_ibfk_1` FOREIGN KEY (`insta_id`) REFERENCES `users`(`insta_id`)
) ENGINE=InnoDB AUTO_INCREMENT=103902 DEFAULT CHARSET=latin1

---喜欢表格---

CREATE TABLE `likes` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`insta_id` bigint(20) unsigned NOT NULL,
`media_id` varchar(50) NOT NULL,
`req_id` bigint(20) unsigned DEFAULT NULL,
`date` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`),
KEY `req_id` (`req_id`),
KEY `insta_id` (`insta_id`),
KEY `media_id` (`media_id`),
CONSTRAINT `likes_ibfk_1` FOREIGN KEY (`req_id`) REFERENCES `like_requests`(`req_id`),
CONSTRAINT `likes_ibfk_2` FOREIGN KEY (`insta_id`) REFERENCES `users`(`insta_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1704209 DEFAULT CHARSET=latin1

我在 likes 表上有一个触发器,定义如下:

CREATE TRIGGER `after_insert_likes` AFTER INSERT ON `likes`
FOR EACH ROW BEGIN
UPDATE users SET users.coin=users.coin+1
WHERE users.insta_id = NEW.insta_id LIMIT 1;
IF NEW.req_id IS NOT NULL THEN
UPDATE like_requests
SET like_requests.remaining_like = like_requests.remaining_like-1
WHERE like_requests.req_id = NEW.req_id
AND like_requests.remaining_like > 0
LIMIT 1;
END IF;
END

做一些简单的插入:

    $sql = "INSERT INTO likes (insta_id,media_id,req_id) VALUES (?,?,?);";
$pdo = $this->db;

$statement = $pdo->prepare($sql);
$statement->bindValue(1,$data['id'],PDO::PARAM_INT);
$statement->bindValue(2,$data['media_id']);
$statement->bindValue(3,$data['req_id'],PDO::PARAM_INT);

try
{
$statement->execute();
return GetOkResponseWithMessage($response,"Like was submitted");
}
catch (PDOException $exc)
{
return GetErrorResponseWithMessage($response,$exc->getMessage(),500);
}

我得到以下死锁错误日志:

*** (1) TRANSACTION:
TRANSACTION 29031910, ACTIVE 1 sec starting index read
mysql tables in use 4, locked 4
LOCK WAIT 7 lock struct(s), heap size 1184, 3 row lock(s), undo log entries 1
MySQL thread id 264238, OS thread handle 0x7f6522c6eb00, query id 753506 localhost xxxx updating
UPDATE users SET users.coin=users.coin+1 WHERE users.insta_id=NEW.insta_id LIMIT 1
*** (1) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 14 page no 1560 n bits 128 index `PRIMARY` of table `insta_star`.`users` trx table locks 4 total table locks 4 trx id 29031910 lock_mode X locks rec but not gap waiting lock hold time 0 wait time before grant 0
*** (2) TRANSACTION:
TRANSACTION 29031909, ACTIVE 1 sec starting index read
mysql tables in use 4, locked 4
7 lock struct(s), heap size 1184, 3 row lock(s), undo log entries 1
MySQL thread id 264237, OS thread handle 0x7f65209f8b00, query id 753507 localhost xxxx updating
UPDATE users SET users.coin=users.coin+1 WHERE users.insta_id=NEW.insta_id LIMIT 1
*** (2) HOLDS THE LOCK(S):
RECORD LOCKS space id 14 page no 1560 n bits 128 index `PRIMARY` of table `insta_star`.`users` trx table locks 4 total table locks 4 trx id 29031909 lock mode S locks rec but not gap lock hold time 0 wait time before grant 0
*** (2) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 14 page no 1560 n bits 128 index `PRIMARY` of table `insta_star`.`users` trx table locks 4 total table locks 4 trx id 29031909 lock_mode X locks rec but not gap waiting lock hold time 0 wait time before grant 0
*** WE ROLL BACK TRANSACTION (2)

这不应该以锁等待而不是死锁结束吗?

如何在不重启交易的情况下解决这个问题?

最佳答案

线程 2 持有用户表中行的共享锁。

然后线程1尝试获取同一行的排他锁,进入锁等待。

但是线程 1 不会有机会超时,因为线程 2 然后试图将他的锁升级为独占...但是要做到这一点,他必须等待处于锁等待状态的线程 1,但是它正在等待线程 2。

他们互相阻挡。

这是一个僵局。

服务器选择要终止的事务,这样它们就不会不必要地相互阻塞。

死锁检测允许一个线程以另一个线程为代价立即成功。否则他们都会被困在等待中,直到其中一个因等待时间过长而死亡。


您处于自动提交模式,当然,这并不意味着您不在事务中。 InnoDB 的每个查询仍然在事务中处理,但使用自动提交时,事务在查询开始执行时隐式启动,并在查询成功时隐式提交。

关于带有引发触发器的插入的 MySQL 死锁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40618799/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com