gpt4 book ai didi

mysql - 使用主键更新行时 MariaDB 死锁

转载 作者:行者123 更新时间:2023-11-29 07:25:46 24 4
gpt4 key购买 nike

更新用户在表中的最后一个事件时,MariaDB(10.1) 出现死锁。

导致错误的查询是

UPDATE auth_sessions SET last_activity_time='2018-12-21 05:45:39 WHERE id= 481;

每当用户对应用程序执行任何操作时,我们都会在过程中执行此查询。

下面是从 show engine innodb status ;

收到的状态
------------------------
LATEST DETECTED DEADLOCK
------------------------
2018-12-21 05:45:39 7fe5b8e6eb00
*** (1) TRANSACTION:
TRANSACTION 3742528, ACTIVE 0 sec starting index read
mysql tables in use 1, locked 1
LOCK WAIT 12 lock struct(s), heap size 2936, 81 row lock(s)
MySQL thread id 1941, OS thread handle 0x7fe5b5df4b00, query id 43106 localhost 127.0.0.1 root updating
UPDATE auth_sessions
SET last_activity_time= NAME_CONST('time_now',_latin1'2018-12-21 05:45:39' COLLATE 'latin1_swedish_ci')
WHERE id= NAME_CONST('temp_session_id',481)
*** (1) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 470949 page no 6 n bits 160 index `PRIMARY` of table `xfusion_auth_engine`.`auth_sessions` trx table locks 5 total table locks 5 trx id 3742528 lock_mode X locks rec but not gap waiting lock hold time 0 wait time before grant 0
*** (2) TRANSACTION:
TRANSACTION 3742527, ACTIVE 0 sec starting index read
mysql tables in use 1, locked 1
12 lock struct(s), heap size 2936, 81 row lock(s)
MySQL thread id 1943, OS thread handle 0x7fe5b8e6eb00, query id 43123 localhost 127.0.0.1 root updating
UPDATE auth_sessions
SET last_activity_time= NAME_CONST('time_now',_latin1'2018-12-21 05:45:39' COLLATE 'latin1_swedish_ci')
WHERE id= NAME_CONST('temp_session_id',481)
*** (2) HOLDS THE LOCK(S):
RECORD LOCKS space id 470949 page no 6 n bits 160 index `PRIMARY` of table `xfusion_auth_engine`.`auth_sessions` trx table locks 5 total table locks 5 trx id 3742527 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 470949 page no 6 n bits 160 index `PRIMARY` of table `xfusion_auth_engine`.`auth_sessions` trx table locks 5 total table locks 5 trx id 3742527 lock_mode X locks rec but not gap waiting lock hold time 0 wait time before grant 0
*** WE ROLL BACK TRANSACTION (2)
------------
TRANSACTIONS

表架构 - 身份验证 session

CREATE TABLE `auth_sessions` (
`id` INT(11) NOT NULL AUTO_INCREMENT COMMENT 'Auto Increment ID',
`user_id` VARCHAR(255) NULL DEFAULT NULL COMMENT 'User Email',
`user_key` VARCHAR(255) NULL DEFAULT NULL COMMENT 'User Key',
`application_key` VARCHAR(255) NULL DEFAULT NULL COMMENT 'Application Key',
`created` DATETIME NULL DEFAULT NULL COMMENT 'Session Creation Time',
`expires` DATETIME NULL DEFAULT NULL COMMENT 'Session Expiration Time',
`is_logged_in` TINYINT(4) NULL DEFAULT NULL COMMENT 'Tells whether user is logged in or not ',
`session_key` VARCHAR(255) NULL DEFAULT NULL COMMENT 'Session Key per user per application key',
`last_activity_time` DATETIME NULL DEFAULT NULL COMMENT 'Last recorded time for any activity',
`session_key_bin` BINARY(16) NULL DEFAULT NULL COMMENT 'Binary ID of Session Key',
PRIMARY KEY (`id`),
INDEX `ix_session_key_bin` (`session_key_bin`)
)

COLLATE='latin1_swedish_ci'
ENGINE=InnoDB

;

有解决此问题的线索或解决方案吗?

最佳答案

如果您有一个多语句交易(您没有):

有时解决类似问题的方法是拥有

SELECT ... WHERE id= 481  FOR UPDATE;

UPDATE 之前,但在事务内。

并不是所有的死锁都是可以避免的。最好准备好处理死锁。这个特定的可能可以通过以下方式正确处理:

计划 A(首选):重播 UPDATE

B 计划(可能没问题,考虑到查询的目的):忽略死锁。

C 计划(我不知道它是否可行;如果可行,应该消除这种死锁):

UPDATE auth_sessions
SET last_activity_time = NOW()
WHERE last_activity_time != NOW()
AND id = 481;

这个想法是为了避免在值已设置为所需时间时尝试更新。

关于mysql - 使用主键更新行时 MariaDB 死锁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53880036/

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