6

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?

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
silversmurf
  • 485
  • 4
  • 11
  • This doesn't *answer* the question, but you could try to use `set_error_handler` and/or `register_shutdown_function` to close the MySQL connection. I'm not sure if those would catch fatal errors, though. Just a thought. UPDATE: Maybe this can help: http://stackoverflow.com/a/8527924 – gen_Eric Mar 12 '14 at 14:21

2 Answers2

7

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.

xdazz
  • 158,678
  • 38
  • 247
  • 274
3

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.

arkascha
  • 41,620
  • 7
  • 58
  • 90