The MySQL client library can perform an automatic reconnect 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.
If it is important for your application to know that the
connection has been dropped (so that is can exit or take action to
adjust for the loss of state information), be sure to disable
auto-reconnect. This can be done explicitly by calling
mysql_options() with the
MYSQL_OPT_RECONNECT option:
my_bool reconnect = 0; mysql_options(&mysql, MYSQL_OPT_RECONNECT, &reconnect);
In MySQL 5.0, auto-reconnect was enabled by default
until MySQL 5.0.3, and disabled by default thereafter. The
MYSQL_OPT_RECONNECT option is available as of
MySQL 5.0.13.
Some client programs might provide the capability of controlling
automatic reconnection. For example, mysql
reconnects by default, but the --skip-reconnect
option can be used to suppress this behavior.
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.
All TEMPORARY tables are closed (and
dropped).
Session variables are reinitialized to the values of the
corresponding variables. This also affects variables that are
set implicitly by statements such as SET
NAMES.
User variable settings are lost.
Prepared statements are released.
HANDLER variables are closed.
The value of LAST_INSERT_ID()
is reset to 0.
Locks acquired with GET_LOCK()
are released.
mysql_ping() does not attempt
a reconnection if the connection is down. It returns an error
instead.

User Comments
Add your own comment.