Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does PHP close a mysql(i) connection on an fatal error?

Tags:

php

mysql

I know PHP closes any open MySQL connection at the end of the script execution, but what happens with connections if a fatal error occurs?

  • Do PHP close connections regardless of an error?
  • Or are they closed after an amount of time?
  • Or is it not opened in that case?

I can't find anything here or on Google.

I use static connections to reuse them. Additionally there is a __destruct() to close it. On the end of the execution, the destructor is called (I see it in the log-file). But if I do a fatal error (just to find out what happens), the destructor is NOT called. What happens with the connection?

like image 447
silversmurf Avatar asked Mar 12 '14 14:03

silversmurf


2 Answers

Unless you use persistent connection, mysql connection will always be automatically closed at the end of the script's execution.

PHP Fatal error will stop the script's execution, and mysql connection will be released.

like image 128
xdazz Avatar answered Oct 01 '22 12:10

xdazz


The php engine removes any open connections in case of such a crash. Just as when there is no crash. This is a cleanup thing the engine does. Exception: when using presistent connections then the connection is pooled and reused by subsequent scripts.

The destructor you mention is a destructor in your class, probably some wrapper class. There is no reason why php should call that if it closes the connection to the mysql server.

like image 32
arkascha Avatar answered Oct 01 '22 12:10

arkascha