views:

128

answers:

2

Hi!

When using prepared statements inside stored procedures, should they be deallocated at the end of the procedure or not, or does it not matter, and why?

Some code to explain:

CREATE PROCEDURE getCompanyByName (IN name VARCHAR(100))
NOT DETERMINISTIC
BEGIN
  PREPARE gcbnStatement FROM 'SELECT * FROM Companies WHERE name=? LIMIT 1';
  SET @companyName = name;
  EXECUTE gcbnStatement USING @companyName;
  DEALLOCATE PREPARE gcbnStatement;
END $$

So - should the DEALLOCATE statement be there or not? Cheers!

/Victor

+2  A: 

According to the MySQL docs:

A prepared statement is specific to the session in which it was created. If you terminate a session without deallocating a previously prepared statement, the server deallocates it automatically.

So, no, I wouldn't bother doing it explicitly, unless you have very long-running sessions.

skaffman
Thanks... While it's not much of a "why" it's certainly the best answer of the lot ;)
Victor
+1  A: 

If you use connection pooling, it is definitely a good idea to deallocate them.

Ok, thanks. Informative.
Victor