tags:

views:

309

answers:

1

My application is using ADODB to connect simultaneously to 2 MySQL DB. Application is downloading large files; it takes a lot of time so "mysql has gone away" error takes place.

I know that MySQL allows to use automatic reconnection which could be enabled using mysql_options(), but I have no clue how to apply this function to ADODB adapter.

    $DB = NewADOConnection('mysql');
    $DB->Connect(DB_HOST, DB_LOGIN, DB_PASSWORD, DB_DBNAME);

Thank you for you time!

PS: Probably I should ask how to get DB handler variable from $DB? If I could get DB connection handler from ADODO as $handler I could use mysql_options($handler, MYSQL_OPT_RECONNECT, 1); But how could I get $handler if I should call mysql_options() before connection (according to MySQL reference http://dev.mysql.com/doc/refman/5.0/en/mysql-options.html )

A: 

To set mysql options, you can use dsn. This is my connection template that I always use:

$options = '';
if ($driver == 'mysql' OR $driver == 'mysqli')
{
  if ($params['pconnect'] === TRUE)
  {
    $options .= '?persist';
  }
  $flags = MYSQL_CLIENT_COMPRESS;
  $options .= (empty($options)?'?':'&')."clientflags=$flags";
}

$dsn = "{$driver}://{$username}:{$password}@{$hostname}/{$database}{$options}";

if ($driver == 'sqlite')
{
  if ($params['pconnect'] === TRUE)
  {
    $options = '?persist';
  }
  $database = urlencode($database);
  $dsn = "{$driver}://{$database}{$options}";
}

$adodb =& ADONewConnection($dsn);

if ($adodb)
{
  //set fetch mode
  $adodb->SetFetchMode(ADODB_FETCH_BOTH);

  //character set
  if ($driver == 'mysql' OR $driver == 'mysqli')
  {
    if (isset($params['char_set']) AND $params['char_set']
        AND isset($params['dbcollat']) AND $params['dbcollat'])
    {
      $charset    = $adodb->qstr($params['char_set']);
      $collation  = $adodb->qstr($params['dbcollat']);
      $adodb->Execute("SET NAMES $charset COLLATE $collation");
    }
  }
  if ($debug)
  {
    @ob_start();
    $adodb->debug = TRUE;
  }
}

You can omit the sqlite driver conditional block if you will only use this for MySQL. Apparentlt, PHP MySQL client only support limited option, here is the list. You can try MySQLi, it have more options.

Have you try the persistent connection istead?

Donny Kurnia
MySQLi lib is exactly what I was looking for. Don't you know is ADODB supports MySQLi?
Kirzilla
Thank you. I've found MySQLi driver in ADODB drivers. Will try right now.
Kirzilla
To know what drivers in ADODb, just take a look inside `drivers` folder. The driver file name follow this format: `adodb-[DRIVER_NAME].inc.php`
Donny Kurnia