The MySQL client library can perform an automatic reconnection to the server if it finds that the connection is down when you attempt to send a statement to the server to be executed. In this case, the library tries once to reconnect to the server and send the statement again.
Some client programs might provide the capability of controlling
automatic reconnection. For example, mysql
reconnects by default, but the
option can be used to suppress this behavior.
If an automatic reconnection does occur (for example, as a result
mysql_ping()), there is
no explicit indication of it. To check for reconnection, call
mysql_thread_id() to get the
original connection identifier before calling
mysql_ping(), then call
mysql_thread_id() again to see
whether the identifier has changed.
Automatic reconnection can be convenient because you need not implement your own reconnect code, but if a reconnection does occur, several aspects of the connection state are reset and your application will not know about it. The connection-related state is affected as follows:
Any active transactions are rolled back and autocommit mode is reset.
All table locks are released.
TEMPORARY tables are closed (and
Session variables are reinitialized to the values of the
corresponding variables. This also affects variables that are
set implicitly by statements such as
User variable settings are lost.
Prepared statements are released.
HANDLER variables are closed.
The value of
is reset to 0.
Locks acquired with
If the connection drops, it is possible that the session
associated with the connection on the server side will still be
running if the server has not yet detected that the client is no
longer connected. In this case, any locks held by the original
connection still belong to that session, so you may want to kill
it by calling