php - 如何解决msyql语句链接超时?
为情所困
为情所困 2017-05-16 13:10:50
0
3
689

我用thinkphp框架
在命令行模式下
写了一个死循环
我要保证我的逻辑操作
永远不会中断
而且每秒执行一次
代码如下

public function index()
{
    $redis   = new \Redis\Redis();//实例化redis
    $redis_res = $redis -> ping();
    if ($redis_res !== 'PONG') {
        error_log('/tmp/1.log', $e->getMessage());
    }
    $abb = M('abb');//实例化
    $i   = 0;
    while(true) {
        //用来处理链接数据库超时 方法1
        $i++;
        if ($i >= 1000) {
            $i = 0;
            $abb = M('abb');
        }
        
        //////////逻辑操作///////////           
        try {
            $result1 = $abb->add(array('a' => 'abc'));//操作数据库
        }  catch (\Exception  $e) {
          error_log('/tmp/2.log', $e->getMessage());
        }
        try {
            $result2 = $redis -> Lpush('abc',$result1);//操作redis
        }  catch (\Exception  $e) {
          error_log('/tmp/3.log', $e->getMessage());
        }
        //////////逻辑操作///////////
        sleep(1);
    }
}

但是有一个问题 我这个链接数据库时间过长会MySQL会自动断掉
2006:MySQL server has gone away
wait_timeout默认是8小时,
去增加wait_timeout 也不是长久之计,
所以我想执行1000次时重新M('abb') ,但好像还是不行,重新M('abb')不是重新练=链接数据库吗?

有没有其他解决方案?
如何保证我的链接数据库不会中断?
redis操作有链接超时的问题吗?
求指导!

为情所困
为情所困

reply all(3)
某草草

It is best to reconnect to the database every time. Your error may be caused by a lost link.

刘奇

Adjust the parameter values ​​of c3p0 related to the database so that it cannot be too different from the database wait_timeout

淡淡烟草味

Just use the mysql_ping function to determine whether it is connected:

    if (!mysql_ping ($conn)) {
       mysql_close($conn);
       $conn = mysql_connect('localhost','user','pass');
       mysql_select_db('db',$conn);
    }
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!