Using last_insert_id (), is it credible if multiple users are entering the database at the same time?

2

I have a question regarding the last_insert_id() method of mysql. And the following: I am creating a procedure to insert into 3 tables, example:

  

person (idPerson, first name, last name)

     

employee (idEmpregado, idPessoa, cargo)

     

contact (idContact, idP, cell)

So, my procedure inserts data into these two tables and I'm using last_insert_id() to retrieve the idPessoa of the Person table, to use both employee and contact tables.

CREATE DEFINER=user'@'localhost PROCEDURE inserirPessoa (var_nome
 varchar(45), var_apelido varchar(45), var_cargo varchar(45), var_celular bigint)

BEGIN

    INSERT INTO pessoa (nome, apelido) values (var_nome, var_apelido);


    SELECT LAST_INSERT_ID() into @idPessoa;


    INSERT INTO candidato (idPessoa, Cargo) values (@idPessoa, var_Cargo);


    INSERT INTO contacto (idPessoa, celular) values (@idPessoa, var_celular);

END

The code works fine. My doubt is, considering the worst case, if more than one user submits the data, will idPessoa not be vulnerable to being recovered by the wrong employee? How does this last_insert_id() work?

    
asked by anonymous 26.01.2016 / 14:48

1 answer

3

The MySQL manual makes it clear that the LAST_INSERT_ID is per connection, so it's perfectly safe to use it the way you're using regardless of any transaction isolation setting.

    
26.01.2016 / 15:42