It is possible for a Mysql Trigger to insert wrong values

Asked

Viewed 33 times

0

I have this Sp that updates the stock:

CREATE DEFINER=`comandas`@`%` PROCEDURE `SP_AtualizaEstoque`( id_prod int, qtde_comprada int, preco_vend decimal(9,2))
BEGIN
    declare contador int(11);
    declare qtd_atual int(11);
    DECLARE qtd_estoque INT (4);


   SELECT count(*) into contador FROM estoque WHERE id_produto = id_prod;
    IF contador > 0 THEN

                    SELECT qtde INTO qtd_estoque FROM estoque WHERE id_produto = id_prod;
                    IF qtd_estoque >= (qtde_comprada*-1) THEN
                         UPDATE estoque SET qtde=qtde + qtde_comprada WHERE id_produto = id_prod;
                    ELSE 
                       If qtd_estoque>0 THEN
                        UPDATE estoque SET qtde=0 WHERE id_produto = id_prod;
                       END IF;

                    END IF;

    ELSE
        INSERT INTO estoque (id_produto, qtde, preco_venda) values (id_prod, qtde_comprada, preco_vend);
    END IF;
END

I have a table called saida_product inside it has 3 Trigger to trigger the Procedure above

one of them is the one that is triggered in the after Insert:

 CALL SP_AtualizaEstoque (new.id_produto, new.qtde * -1, new.preco_venda);

So far so good. What happens when I’m testing in the test environment works perfectly, but when the system is being used networked on 4 computers at the same time, some products in the product table are inserted with wrong values. I’ve never seen it happen, example: I bought a Coca with id 56, and the output product is registered with id 34 that falls into another product. Then the stock gets all messed up. This does not happen directly, it happens sometimes when the system is highly in use in the restaurant. I use the network system where Mysql is installed on the server, and the system was programmed C#. Someone give me a help or some suggestion?

  • 1

    And what the ISOLATION LEVELof your transaction? Probably the ISOLATION LEVEL SERIALIZABLE meets your needs.

  • this by default the REPEATABLE READ. I will try ISOLATION LEVEL SERIALIZABLE, thanks for the reply.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.