I have a stored procedure that generates a UID from the ticket table, but when I load it, I get a lot of dead ends. I call this procedure many times from several concurrent connections when my task needs a new UID.
BEGIN DECLARE a_uid BIGINT(20) UNSIGNED; START TRANSACTION; SELECT uid INTO a_uid FROM uid_data FOR UPDATE;
I really considered using:
BEGIN REPLACE INTO uid_data (stub) VALUES ('a'); SELECT LAST_INSERT_ID(); END
However, I was not sure that it would be safe with simultaneous connections, since there is no blocking, unlike the first procedure with SELECT FOR UPDATE
.
Here is the table:
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 | | +-------+---------------------+------+-----+---------+----------------+
I have configured transaction isolation in transactions:
mysql> SHOW VARIABLES LIKE 'tx_isolation'; +---------------+-----------------+ | Variable_name | Value | +---------------+-----------------+ | tx_isolation | READ-COMMITTED | +---------------+-----------------+
This is what I get from 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)
I would be grateful if someone could explain what is happening and how they can be avoided.
source share