Exception Handling In Database Connection

Hello every one,

Is there any way that i can redirect to some page on database connection error. I am trying to do this error handling since 2 days but i did not get success.

In my config file following code is specified.


// use 'site/error' action to display errors




'connectionString' => 'mysql:host=;dbname=test',

'emulatePrepare' => true,

'username' => 'root',

'password' => '123',

'charset' => 'utf8',


But on database connection error this "actionError" is not called.

Please guide me.

in your site controller just put the action

 public function actionError() {

        if ($error = Yii::app()->errorHandler->error) {

            if (Yii::app()->request->isAjaxRequest)

                echo $error['message'];


                $this->render('error', $error);



Thanks Maggie Q for the quick reply,

This function is already available in my SiteController. I was checking the access_log but i don’t see any entry for the site/error in there. So may be this function is not being called somehow.

Specific error i am getting is as followed.

CDbConnection failed to open the DB connection: SQLSTATE[HY000] [2013] Lost connection to MySQL server at ‘reading initial communication packet’, system error: 111

Thanks in advance.

Try the solution at


Errors are handled differently in production mode. The Yii tutorial has more info on this.

check this wiki

also check your DB like


            'connectionString' => 'sqlite:protected/data/blog.db',

            'tablePrefix' => 'tbl_',


        // uncomment the following to use a MySQL database



            'connectionString' => 'mysql:host=localhost;dbname=vqs',

            'emulatePrepare' => true,

            'username' => 'root',

            'password' => '',

            'charset' => 'utf8',

            'tablePrefix' => 'tbl_',


Thanks a lot BlkRaven…

You made my day…:)