Currently I have a problem apparently with some triggers in one of my tables as it is blocked, by doing a few "INSERTS" the table works correctly but by doing many simultaneous INSERTS (I'm using JMeter for load tests) a DeadLock
appears in the table ..
In the table there are 2 types of Trigger AFTER INSERT
and BEFORE INSERT
in AFTER INSERT
there are many things that are done and remove it temporarily, just leave the BEFORE INSERT
and likewise there is blocking in the table.
Here is the trigger code:
DELIMITER
CREATE
TRIGGER 'getNextIdTicket' BEFORE INSERT ON 'pos_doc_ticket'
FOR EACH ROW
BEGIN
IF (new.id_cat_producto = 0 AND new.buscaNuevoRegistro=TRUE)
THEN
SET new.id_doc_ticket = getNextSeq (
new.id_cat_zona,
new.id_cat_empresa,
new.id_cat_tienda,
"idTicket"
);
END IF;
IF new.id_cat_producto>0
THEN
SET new.id_linea = getNextSeqLine (
new.id_cat_zona,
new.id_cat_empresa,
new.id_cat_tienda,
new.id_cat_caja,
new.id_doc_ticket
);
END IF ;
END;
DELIMITER ;
And the function you call is this:
DELIMITER
CREATE FUNCTION 'getNextSeq'(idZona BIGINT, idEmpresa BIGINT, idTienda
BIGINT, sSeqName VARCHAR(50)) RETURNS BIGINT(10) UNSIGNED
BEGIN
DECLARE nLast_val BIGINT;
IF idTienda > 0 THEN
SET nLast_val = (SELECT seq_val
FROM _sequence
WHERE seq_name = sSeqName);
ELSE
SET nLast_val = (SELECT seq_val
FROM _sequence
WHERE seq_name = sSeqName);
END IF;
IF (nLast_val IS NULL OR nLast_val = 0)THEN
SET nLast_val = 1;
INSERT INTO _sequence (id_cat_zona,id_cat_empresa,id_cat_tienda,seq_name,seq_val)
VALUES (idZona,idEmpresa,idTienda,sSeqName,nLast_Val);
ELSE
SET nLast_val = nLast_val + 1;
UPDATE _sequence SET seq_val = nLast_val
WHERE seq_name = sSeqName;
END IF;
RETURN nLast_val;
END
DELIMITER ;
As you can see I use it to obtain a consecutive id to insert it in the table before the INSERT but when I do around 100 simultaneous requests when arriving at the INSERTS there are several blockages of the table by which not all the INSERTS they end successfully.
I hope you can help me and see what solution may exist.