Hi Guys,
I wanted to change PHP PDO::ATTR_TIMEOUT value and since I didn’t find this in any config options I inherited class CDbConnection and achived to set custom timeout duration.
This is very userfull to achive high number of TPS & throughput from Application.
<?php
/**
* EDbConnection class file
*
* @author Mayur Ahir <ahirmayur@gmail.com>
* @link http://www.mayurahir.com
* @copyright Copyright © 2013 e|antrix Labs.
* @license GNU GPLv3
*/
/**
* This class is extension of CDbConnection for optimizing
* High load servers and passing default/additional config params
*/
class EDbConnection extends CDbConnection {
/**
* @var integer which specifies the timeout duration in seconds.
* Not all drivers support this option, and it's meaning may differ from driver to driver.
* For example, sqlite will wait for up to this time value before giving up on obtaining an writable lock,
* but other drivers may interpret this as a connect or a read timeout interval
* Default value is 300.
* @see http://php.net/manual/en/pdo.setattribute.php
*/
public $pdoTimeout = 300;
/**
* Initializes the open db connection.
* This method is invoked right after the db connection is established.
* The default implementation is to set the charset for MySQL and PostgreSQL database connections.
* @param PDO $pdo the PDO instance
*/
protected function initConnection($pdo) {
parent::initConnection($pdo);
$pdo->setAttribute(PDO::ATTR_TIMEOUT, $this->pdoTimeout);
}
}
This class can be used in app config as following (other params remain identical to CDbConnection),
.
.
'components' => array(
'db' => array(
'class' => 'EDbConnection',
'connectionString' => 'mysql:host=127.0.0.1;dbname=test',
'username' => 'db_user',
'password' => 'db_password',
'charset' => 'utf8',
'pdoTimeout' => 15,
.
.
),
.
.
);
.
.
Feel free to edit the code and use as per your needs. Comments are most welcome.