码迷,mamicode.com
首页 > 数据库 > 详细

Yii MySQL server has gone away

时间:2015-05-07 16:44:19      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:

 场景:

长时间数据库操作致使DB会话超时,多发在后台脚本,可以预估脚本执行时间,在每次执行前重新获取DB预防程序中断 


/**

     * Get new mysql conn
     * @param $timeout int default 1440s optional
     * @return object 
     **/
    public static function getNewDb($timeout = 1440) {


        if(self::$db)
            self::CloseConnection();
        if (self::$db == null)
        {
            self::$db = Yii::app()->getComponent(‘database‘);
            if (self::$db instanceof CDbConnection)
            {
                self::$db->setActive(true);
                self::$db->createCommand(‘set session wait_timeout = ‘. $timeout)->execute();
                return self::$db;
            }
            else
                throw new CDbException(Yii::t(‘yii‘,‘Active Record requires a "db" CDbConnection application component.‘));
        }
        return self::$db;


    }


    /**
     * Closed specific conn
     * @return void
     **/
    public static function CloseConnection() {

        if(self::$db !== null) {
            self::$db->setActive(FALSE);
            self::$db = null;
        }

    }

Yii MySQL server has gone away

标签:

原文地址:http://blog.csdn.net/lucifer_qiao/article/details/45562365

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!