phpcode

CakePHP MySQLi Connection Timeout

So, we’ve been wrestling a little with cron jobs that take a long time to do their thing and having CakePHP’s DBO lose connection to the DB. It never checks to see if there’s an active connection prior to saving/updating data and errors out. So today I’ve taken the time to do what I can and fix this annoying problem.

Here’s a little patch.

cake/libs/model/datasourcesd/bodbo_mysqli.php

function _execute($sql) {
	mysqli_ping($this->connection);
	if (preg_match('/^s*call/i', $sql)) {
		return $this->_executeProcedure($sql);
	}
	return mysqli_query($this->connection, $sql);
}

etc/php.d/php.ini

[MySQLi]
	mysqli.max_links = -1
	mysqli.default_port = 3306
	mysqli.default_socket =
	mysqli.default_host = localhost
	mysqli.reconnect = On

mysqli.reconnect integer
Automatically reconnect if the connection was lost.

mysqli::ping mysqli_ping
(PHP 5)

mysqli::ping — mysqli_ping — Pings a server connection, or tries to reconnect if the connection has gone down

mysqli_ping() will attempt to ping the server. If there’s no connection.. mysqli_reconnect will establish a new connection and all is well again in CakePHP.

No comments yet.

Leave a Reply