存储过程中的SELECT有问题。
隔离级别为READ-COMMITED,引擎为InnoDB,因此所有选择均应为非锁定一致读取:https://dev.mysql.com/doc/refman/8.0/en/innodb-consistent-read.html
数据库是MariaDB 10.1版:
MariaDB [(none)]> select @@version;
+-----------------+
| @@version |
+-----------------+
| 10.1.24-MariaDB |
+-----------------+
1 row in set (0.02 sec)
在存储过程锁中进行选择之后,将获得对表agent
的锁定:
IF
v_pack_agent_id IS NULL OR
v_pack_agent_id not in
(
select ID from agent where LINKED_AGENTS_GUID = (select LINKED_AGENTS_GUID from agent where ID = a_agent_id) union all
select a_agent_id
)
THEN
SET a_sign = SIGN_NOT_OWN_BLOCK;
LEAVE l_proc;
END IF;
表agent
的定义:
MariaDB [db_mercury]> show create table agent\G
*************************** 1. row ***************************
Table: agent
Create Table: CREATE TABLE `agent` (
`ID` smallint(5) unsigned NOT NULL AUTO_INCREMENT COMMENT 'PK',
...
`LINKED_AGENTS_GUID` varchar(64) COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`ID`),
UNIQUE KEY `UK$ORA_ID` (`ORA_ID`),
KEY `IDX_LINKED_AGENTS_GUID` (`LINKED_AGENTS_GUID`)
) ENGINE=InnoDB AUTO_INCREMENT=6198 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
问题是因为select嵌套在IF
块中,如果我将select移到IF
之外,如下所示,则不会获得锁定。
这不会锁定任何内容:
select ID
from agent
where LINKED_AGENTS_GUID = (select LINKED_AGENTS_GUID
from agent
where ID = a_agent_id)
union all
select a_agent_id
对我来说,这似乎是一个错误。
有什么想法为什么选择锁定?