3

Should I manage the table unlock in case of an error in php (try/catch)? Or the lock is automatically released by PDO object ad the end of the script?

Tobia
  • 9,165
  • 28
  • 114
  • 219

1 Answers1

2

Yes. Unless you're using a persistent connection, on the scripts's termination PDO will close the connection, and mysql, in turn will release all locks:

PHP will automatically close the connection when your script ends.
http://php.net/manual/en/pdo.connections.php

If the connection for a client session terminates, whether normally or abnormally, the server implicitly releases all table locks held by the session (transactional and nontransactional).
https://dev.mysql.com/doc/refman/5.1/en/lock-tables.html

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • 1
    Just for the record: It is always a good practice to release all resources (including locks in databases) when they are not in use anymore or at the end of the script or during error handling. – Pred Oct 16 '15 at 07:50
  • I think that the PHP PDO is a black box, I really can not understand what is happening about mysql connections and sessions... – Tobia Oct 16 '15 at 08:49