Jump to content


Closing a DB connection when not need

close db connection

  • Please log in to reply
3 replies to this topic

#1 halben

  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts

Posted 12 September 2013 - 07:17 PM



I'm new to this forum and I'm looking for some assistance.




I have the following code: (just a quick example)




try {

      // import Soap client



      // connect to 3rd party DB

      $3rd_db_client = new Something($one, $two, $three);


     // Do some more stuff


} catch(exception $e) {

  echo $e->getMessage() ;







My question is, does the catch{ exit; } close the DB connection? 

#2 Drongo_III

  • Members
  • PipPipPip
  • Advanced Member
  • 566 posts

Posted 12 September 2013 - 07:41 PM

Generally a connection to a database will close when the script stops executing.

So exiting the script would in theory close the connection.

However there are database specific functions for closing the connection which are more appropriate - e.g mysqli_close. Equivalents exist for pdo etc.

Learning something new simply uncovers how much there is left to learn...

One day when I know what I'm talking about all my noob posts will come back to haunt me. But I shall rejoice in the cleansing light of knowledge over ignorance :)

#3 Irate

  • Members
  • PipPipPip
  • Advanced Member
  • 358 posts
  • LocationHamburg, Germany
  • Age:17

Posted 12 September 2013 - 07:58 PM

Only if you get an error in your try block.
Use a finally { exit; } block.
Quod placet mihi non placeat tibi. - What I think to be good must not always equal your perception of it.

I am not perfect. I try a lot with the code I provide and I don't guarantee for it to work as I have mostly no option to test it on my mobile phone. I do apologize for any inconvenience I caused, but if I do happen to have helped, liking my posts or marking them as to have solved or answered your question would be nice.

#4 halben

  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts

Posted 12 September 2013 - 08:56 PM

Thanks guys. Fast reply, I'm liking this forum!

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users