Home  >  Q&A  >  body text

innodb - 如何实现mysql线程安全的nextval()方法

尝试通过建立序列表与函数实现,可以正常返回下条序列,但通过jmeter测试并发100个线程,发现返回的结果中存在重复的记录。

下面是链接中的实现方法

-- 表
CREATE TABLE `sequence_data` (
    `sequence_name` varchar(100) NOT NULL,
    `sequence_increment` int(11) unsigned NOT NULL DEFAULT 1,
    `sequence_min_value` int(11) unsigned NOT NULL DEFAULT 1,
    `sequence_max_value` bigint(20) unsigned NOT NULL DEFAULT 18446744073709551615,
    `sequence_cur_value` bigint(20) unsigned DEFAULT 1,
    `sequence_cycle` boolean NOT NULL DEFAULT FALSE,
    PRIMARY KEY (`sequence_name`)
);
-- 函数
delimiter //
CREATE FUNCTION `nextval` (`seq_name` varchar(100))
RETURNS bigint(20) NOT DETERMINISTIC
BEGIN
    DECLARE cur_val bigint(20);
 
    SELECT
        sequence_cur_value INTO cur_val
    FROM
        sequence_data
    WHERE
        sequence_name = seq_name
    ;
 
    IF cur_val IS NOT NULL THEN
        UPDATE
            sequence_data
        SET
            sequence_cur_value = IF (
                (sequence_cur_value + sequence_increment) > sequence_max_value,
                IF (
                    sequence_cycle = TRUE,
                    sequence_min_value,
                    NULL
                ),
                sequence_cur_value + sequence_increment
            )
        WHERE
            sequence_name = seq_name
        ;
    END IF;
 
    RETURN cur_val;
END

//
delimiter ;

-- insert new sequence
-- INSERT INTO sequence_data (sequence_name) VALUE ('sq_my_sequence');

-- 调用
-- SELECT nextval('sq_my_sequence') as nextval
PHP中文网PHP中文网2742 days ago1113

reply all(1)I'll reply

  • 黄舟

    黄舟2017-04-17 15:37:29

    The default isolation level of mysql is repeatable_read. Before your transaction is committed, other threads cannot read your modifications. You can add commit at the end to submit the update results. If that doesn't work, use row-level locks (select * sequence_data from WHERE sequence_name = seq_name for update). If one thread does not submit, other threads are not allowed to read, so concurrency safety can be guaranteed.

    reply
    0
  • Cancelreply