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!
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: