6

我有一个从“票证”表生成 UID 的存储过程,但是在负载下我遇到了很多死锁。每当我的任务需要新的 UID 时,我都会从多个并发连接中多次调用此过程。

BEGIN
    DECLARE a_uid BIGINT(20) UNSIGNED;
    START TRANSACTION;
    SELECT uid INTO a_uid FROM uid_data FOR UPDATE; # Lock
    INSERT INTO uid_data (stub) VALUES ('a') ON DUPLICATE KEY UPDATE uid=uid+1;
    SELECT a_uid+1 AS `uid`;
    COMMIT;
END

我确实考虑过使用:

BEGIN
    REPLACE INTO uid_data (stub) VALUES ('a');
    SELECT LAST_INSERT_ID();
END

但是,我不确定这对于并发连接是否安全,因为没有锁定,这与使用SELECT FOR UPDATE.

这是表格:

mysql> DESCRIBE uid_data;
+-------+---------------------+------+-----+---------+----------------+
| Field | Type                | Null | Key | Default | Extra          |
+-------+---------------------+------+-----+---------+----------------+
| uid   | bigint(20) unsigned | NO   | PRI | NULL    | auto_increment |
| stub  | char(1)             | NO   | UNI | NULL    |                |
+-------+---------------------+------+-----+---------+----------------+

我已经设置了读取提交的事务隔离:

mysql> SHOW VARIABLES LIKE 'tx_isolation';
+---------------+-----------------+
| Variable_name | Value           |
+---------------+-----------------+
| tx_isolation  | READ-COMMITTED  |
+---------------+-----------------+

这就是我要回来的SHOW ENGINE INNODB STATUS;

...
... dozens and dozens of the following record locks...

Record lock, heap no 1046 PHYSICAL RECORD: n_fields 2; compact format; info bits 32
 0: len 1; hex 61; asc a;;
 1: len 8; hex 00000000000335f2; asc       5 ;;

Record lock, heap no 1047 PHYSICAL RECORD: n_fields 2; compact format; info bits 32
 0: len 1; hex 61; asc a;;
 1: len 8; hex 00000000000335f1; asc       5 ;;

*** (2) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 13 page no 4 n bits 1120 index `stub` of table `my_db`.`uid_data` trx id 13AA89 lock_mode X waiting
Record lock, heap no 583 PHYSICAL RECORD: n_fields 2; compact format; info bits 32
 0: len 1; hex 61; asc a;;
 1: len 8; hex 00000000000334a8; asc       4 ;;

*** WE ROLL BACK TRANSACTION (1)

如果有人能解释发生了什么以及如何避免它们,我将不胜感激。

4

3 回答 3

2

做这个:

CREATE TABLE tickets
(
    uid serial
)

然后获取下一个uid:

BEGIN
  INSERT INTO tickets VALUES (NULL);
  SELECT LAST_INSERT_ID();
END

uid 序列号相当于

uid BIGINT(20) UNSIGNED NOT NULL PRIMARY KEY auto_increment

您不应该使用这种方法遇到任何死锁,并且可以随心所欲地抛出任意数量的连接。

于 2012-09-08T02:16:32.723 回答
0

A deadlock occurs in this scenario:

Transaction 1 : requests a lock (SELECT...FOR UPDATE) and acquires it

Transaction 2 : requests a lock (SELECT...FOR UPDATE) and must wait

Transaction 1 : tries to insert, hits a duplicate, therefore updates (INSERT...ON DUPLICATE KEY UPDATE) => deadlock

I am not too sure about the reason, I suspect it has something to do with the ON DUPLICATE KEY UPDATE. I'm still investigating and will come back if I find out.

[Edit] A deadlock occurs even with:

BEGIN
    START TRANSACTION;
    SELECT uid FROM uid_data FOR UPDATE;
    UPDATE uid_data SET uid = uid +1; -- here, a deadlock would be detected in a blocked, concurrent connection
    COMMIT;
END

What about this:

BEGIN
    START TRANSACTION;    
    UPDATE uid_data SET uid = uid +1;
    SELECT uid FROM uid_data;
    COMMIT;
END

You could drop your stub colum altogether. The only drawback is that you must initialise your uid_data with one row.

于 2012-07-05T15:07:00.200 回答
0

您可以尝试使用

UPDATE uid_data SET uid = LAST_INSERT_ID(uid+1);
SELECT LAST_INSERT_ID();

在像这样的桌子上

CREATE TABLE `uid_data` (
    `uid` BIGINT(20) UNSIGNED NOT NULL
)
COLLATE='utf8_general_ci'
ENGINE=MyISAM;

这是线程安全的,如果是 MyISAM 则不会锁定表(实际更新语句期间除外)。

于 2012-07-20T21:38:57.437 回答