Jump to content


Photo

Closing a DB connection when not need

close db connection

  • Please log in to reply
3 replies to this topic

#1 halben

halben

    Advanced Member

  • Members
  • PipPipPip
  • 42 posts

Posted 12 September 2013 - 02:17 PM

Hi,

 

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

 

 

 

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

 

<?php

 

try {

      // import Soap client

      require_once('/random.php');

     

      // connect to 3rd party DB

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

 

     // Do some more stuff

 

} catch(exception $e) {

  echo $e->getMessage() ;

  exit;

}

 

?>

 

 

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


#2 Drongo_III

Drongo_III

    Advanced Member

  • Members
  • PipPipPip
  • 512 posts

Posted 12 September 2013 - 02: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.

http://php.net/manua...ysqli.close.php
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

Irate

    Advanced Member

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

Posted 12 September 2013 - 02: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

halben

    Advanced Member

  • Members
  • PipPipPip
  • 42 posts

Posted 12 September 2013 - 03: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

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com