1

I have long running CLI script which crashes down sometimes because of:

MySQL Server gone away

#inside loop
$content = null;
while ($content == null) {
  try {
     $content = getContentFromMysql();
   } catch(PDOException $ex) {
     #What to do here?
   }  
}
#do something long with fetched content

What should I write in the catch clause? (I am using standard Drupal DB API)

EDIT

Since error happens when I try to select data I write my own wrapper class (I am using MSSQL as main db, so I can messed up Mysql driver):

class JoomlaDatabaseConnection_mysql extends DatabaseConnection_mysql {
    public function select($table, $alias = null, $options = null) {
        self::openConnection('joomla', 'default');
        return parent::select($table, $alias, $options);       
    }     
}

But know how to tell Drupal to use this class?

:/

final protected static function openConnection($key, $target)

shame not public, but I understand why.


Thanks to @karthik advice I am testing solution with ini_set.

Another solution for altering database access class is to create new driver:

  • Create directory of driver name (e.g. mysqlJoomla) in includes/database
  • copy standard mysql driver files from includes/database/mysql
  • change database.inc file content to e.g.:

    class DatabaseConnection_mysqlJoomla extends DatabaseConnection_mysql {
     public function select($table, $alias = null, $options = null) {
        parent::openConnection('joomla', 'default');
        return parent::select($table, $alias, $options);
     }     
    }
    
  • add new driver info:

    $joomla_url = array(
     'database' => JDB,
     'username' => JUSER,
     'password' => JPASS,
     'host' => JHOST,
     'prefix' => 'jos_',
     'driver' => 'mysqlJoomla',
    );
    Database::addConnectionInfo('joomla', 'default', $joomla_url);
    

EDIT

Oops I see I can't use

openConnection

method inside driver class because it is method from Database class :/

It is a big problem in DB API. Programmer should has chance to reconnect (calling openConnection()) by using inheritance.

  • 2
    You can try setting the default timeout to a higher value see this SO stackoverflow.com/questions/1644432/… – karthik Sep 13 '12 at 13:46
  • @karthik unfortunately this issue is on MySQL side – Codium Sep 13 '12 at 17:10
  • 1
    You are asking two different questions: why you get a "server gone away" error, and how to override a database driver to rewrite one of its methods. Notice that a database driver is written when it is necessary to use a different database engine, not to change the default connection used for a query. – kiamlaluno Sep 13 '12 at 19:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.