php - How to solve msyql statement link timeout?
为情所困
为情所困 2017-05-16 13:10:50
0
3
767

I used the thinkphp framework
in command line mode
to write an infinite loop
I want to ensure that my logical operations
never be interrupted
and executed once per second
>The code is as follows

public function index()
{
    $redis = new \Redis\Redis();//Instantiate redis
    $redis_res = $redis -> ping();
    if ($redis_res !== 'PONG') {
        error_log('/tmp/1.log', $e->getMessage());
    }
    $abb = M('abb');//instantiation
    $i = 0;
    while(true) {
        //Used to handle connection database timeout Method 1
        $i++;
        if ($i >= 1000) {
            $i = 0;
            $abb = M('abb');
        }
        
        //////////Logical operations////////////
        try {
            $result1 = $abb->add(array('a' => 'abc'));//Operation database
        } catch (\Exception $e) {
          error_log('/tmp/2.log', $e->getMessage());
        }
        try {
            $result2 = $redis -> Lpush('abc',$result1);//Operation redis
        } catch (\Exception $e) {
          error_log('/tmp/3.log', $e->getMessage());
        }
        //////////Logical operations////////////
        sleep(1);
    }
}

But there is a problem. MySQL will automatically disconnect if the connection time to the database is too long
2006: MySQL server has gone away
The default wait_timeout is 8 hours,
increasing wait_timeout is not a long-term solution. ,
So I want to re-M('abb') when executing 1000 times, but it still doesn't seem to work. Doesn't re-M('abb') mean re-training = linking to the database?

Are there any other solutions?
How can I ensure that my linked database will not be interrupted?
Is there any link timeout problem in redis operation?
Ask for guidance!

为情所困
为情所困

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