Is there a reason for not supporting PDO::ATTR_PERSISTENT when calling PDO::__construct? I guess this will affect performance only if DB is not @localhost because TCP handshaking adds extra latency. On the other hand, too many idle persistent connections to a DB may hit some limit in the DB. It's best to have ability to configure this in settings.php.

Comments

ogi’s picture

I've made a very quick test that consistently shows that there's big gain in enabling persistent connections when PostgreSQL 8.4 is used. Without persistent connections (default), ab -n 1000 -c 10 shows 75 requests/sec, with persistent connections enabled it shows 90 requests/sec. Even if it's only for PostgreSQL, it's a pretty significant boost made by one-line change.

ogi’s picture

Unfortunately Drupal uses PDO::ATTR_STATEMENT_CLASS that cannot be used with persistent connections :-(

ogi’s picture

Status: Active » Closed (won't fix)
pwaterz’s picture

Actually this is possible

This is how you would do it.

Include this class in your settings.php.

class DatabaseConnection_mysql_persistant extends DatabaseConnection_mysql {
  
  function __construct(array $connection_options = array()) {
    
    //If you unset this, the PDO attribute PDO::ATTR_STATEMENT_CLASS will not be set. Which means you can enable persistant connections
    //These are the comments from the statement class in core

/**
 * Default implementation of DatabaseStatementInterface.
 *
 * PDO allows us to extend the PDOStatement class to provide additional
 * functionality beyond that offered by default. We do need extra
 * functionality. By default, this class is not driver-specific. If a given
 * driver needs to set a custom statement class, it may do so in its
 * constructor.
 *
 * @see http://us.php.net/pdostatement
 */

//Mysql driver does not set this, so we can remove it.

    unset($this->statementClass);
    parent::__construct($connection_options);
  }
  
}

Then in your settings.php


$databases = array (
  'default' =>
  array (
    'default' =>
    array (
      'database' => 'fresh7',
      'username' => 'username',
      'password' => '',
      'host' => 'localhost',
      'port' => '',
      'driver' => 'mysql_persistant',
      'prefix' => '',
      'charset' => 'utf8mb4',
      'pdo' => array(
         PDO::ATTR_PERSISTENT => TRUE,
       ),
    ),
  ),
);

I have not tested this, but in theory it should work

colan’s picture

Issue summary: View changes

I'd recommend that folks read What are the disadvantages of using persistent connection in PDO? before attempting this.

What Drupal does should be good enough, as it maintains the same connection for each page request. I wrote a bit more about this over at Connection Pool.

chi’s picture

Version: 7.0 » 11.x-dev
Status: Closed (won't fix) » Active

shows 75 requests/sec, with persistent connections enabled it shows 90 requests/sec

What Drupal does should be good enough

These two statements contradicts each other.

PostreSQL connection is relatively slow especially when SSL is enabled (that's done by default in many distributions). When page cache is enabled it may happened that ~80% of request time for anonymous visitors is spent on establishing DB connection.

I think it worth reopening.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.