MySQL :: MySQL 8.0 C API Developer Guide :: 3.6.8 Automatic Reconnection Control (original) (raw)

3.6.8 Automatic Reconnection Control

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. If auto-reconnect is enabled, the library tries once to reconnect to the server and send the statement again.

Note

Beginning with MySQL 8.0.34, the automatic reconnection feature is deprecated. The relatedMYSQL_OPT_RECONNECT option is still available but now returns a deprecation warning to the standard error output if your application calls themysql_get_option() ormysql_options() function with the option, even when setting it to false.

Expect automatic reconnection functionality to be removed in a future version of MySQL.

Auto-reconnect is disabled by default.

If the connection has gone down, the effect ofmysql_ping() depends on the auto-reconnect state. If auto-reconnect is enabled,mysql_ping() performs a reconnect. Otherwise, it returns an error.

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.

If an automatic reconnection does occur (for example, as a result of calling mysql_ping()), there is no explicit indication of it. To check for reconnection, callmysql_thread_id() to get the original connection identifier before callingmysql_ping(), then callmysql_thread_id() again to see whether the identifier 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 on the server side and your application will not be notified.

Reconnection affects the connection-related state as follows:

If reconnection occurs, any SQL statement specified by callingmysql_options() with theMYSQL_INIT_COMMAND option is re-executed.

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 mysql_kill().