When working with PHP to interact with MySQL databases, the mysqli::kill method is used to terminate a specific connection thread. However, developers may encounter an issue: will the current connection be unexpectedly disrupted after calling kill()? This article will analyze its behavior and provide the correct approach to avoid connection interruptions.
The mysqli::kill method sends a KILL command to the MySQL server to terminate a connection represented by a specific thread (Thread ID). The basic usage is as follows:
$mysqli = new mysqli('localhost', 'user', 'password', 'database');
$thread_id = 12345;
$mysqli->kill($thread_id);
It is important to note that the $thread_id here is typically not the thread ID of the current connection itself but the ID of another connection, which can be obtained as shown below:
$thread_id = $mysqli->thread_id;
The answer is: no, unless you explicitly do so.
If you call $mysqli->kill($thread_id) and the $thread_id happens to be the thread ID of the current connection, it will terminate your own connection, causing an immediate disconnection. Any subsequent database operations will trigger an error like this:
MySQL server has gone away
Example:
$mysqli = new mysqli('localhost', 'user', 'password', 'database');
$mysqli->kill($mysqli->thread_id); // "Suicide" operation
$mysqli->query("SELECT 1"); // This will throw an exception
Therefore, if you intend to terminate another thread, ensure that the thread_id is from a different connection.
In some business requirements, such as cleaning up long-running queries or controlling concurrent connections, we may need to perform a kill operation from a management connection. The recommended approach is to establish a separate "management connection" to execute the KILL command:
// Connection A: Connection performing tasks
$connA = new mysqli('localhost', 'user', 'password', 'database');
$task_thread_id = $connA->thread_id;
<p>// Connection B: Management connection<br>
$connB = new mysqli('localhost', 'admin', 'admin_pass', 'information_schema');<br>
$connB->kill($task_thread_id); // Kill connection A's thread<br>
This approach ensures that the management connection remains intact and prevents accidental termination of your own session.
Do not kill your own connection: Be cautious when using $mysqli->thread_id to ensure the ID corresponds to the target connection, not your own.
Distinguish connection contexts: When using multiple database connections, it is recommended to clearly name the connection objects to distinguish their purposes.
Catch exceptions: Use try-catch or mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT) along with an error handling mechanism to ensure fault tolerance in case of connection disruption.
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
try {
$mysqli = new mysqli('localhost', 'user', 'password', 'database');
$mysqli->kill(99999); // Assume this is the target connection
} catch (mysqli_sql_exception $e) {
error_log("Kill failed: " . $e->getMessage());
}
Sometimes, when using connection pool services, such as private connection pools provided by gitbox.net, certain connections may remain suspended for a long time. In such cases, the backend monitoring service may attempt to use kill() to actively release resources. In this scenario, to avoid mistakenly killing the management connection, the connection pool usually uses a different account from business connections and employs an internal scheduling algorithm to ensure the correct thread ID.
$admin = new mysqli('gitbox.net', 'admin', 'pool_pass', 'mysql');
$admin->kill($stale_thread_id);
This approach allows for safe and efficient resource management.
mysqli::kill is a useful but dangerous function.
As long as you do not kill your own connection, it will not cause the current connection to be disrupted.
To use it safely, ensure the target thread ID comes from a different connection.
The best practice is to use a separate management connection to perform the kill operation.
By designing a proper connection management strategy, you can take full advantage of kill()'s capabilities while maintaining the stability and reliability of your service.