Flourish PHP Unframework
This is an archived copy of the forum for reference purposes

How to be sure an fDatabase instance is still connected?

posted by peranme 9 years ago

Hi!

I'm trying to write a function that'll tell me if the fDatabase instance I'm using is still connected (so I can reconnect if that's not the case), and this is what I have so far:

function IsConnectedToDb(&$db){
  $strDbName="my_db";
  $boolIsConnected=false;
  if(is_object($db)){
    if($db instanceof fDatabase){
      if($db->getDatabase()==$strDbName){        
        return true;
      }
    }
  }
  return false;
}

(I suspect the & in the declaration might be superfluous, but maybe it could save RAM?)

What this does is check the name of the database, but is that ACTUALLY the same thing as checking if the connection is still alive? Is there a shorter/better/faster way of doing it?

TIA!

Per.

To my knowledge there isn't anything built in that will accomplish this. Depending on what extension is being used, fDatabase::getConnection() will return the connection resource or object. You might be able to use this, but I am not sure how every database extension handles disconnection. fDatabase would probably need to be enhanced to detect how each extension presents this information.

fDatabase::getDatabase() will just return the name of the database, so that will definitely not help.

Just out of curiosity, is your database connection timing out on you because you are running a long-lived process?

posted by wbond 9 years ago

Hi again!

Yeah, this is a daemon process I've got going to do some stuff related to a web service in the background, and at the time it seemed like a good idea to write it in PHP and run it from CLI.

I've got it restarting after having done a set number of "jobs", but sometimes it'll run for as much as 30-40 hours without restart, and I'd like to be sure the connection is still alive somehow, if that's at all possible. 8-)

posted by peranme 9 years ago