Rechercher dans le manuel MySQL
28.7.28 C API 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.
Auto-reconnect is disabled by default.
If it is important for your application to know that the
connection has been dropped (so that it can exit or take action to
adjust for the loss of state information), be sure that
auto-reconnect is disabled. To ensure this, call
mysql_options()
with the
MYSQL_OPT_RECONNECT
option:
bool reconnect = 0;
mysql_options(&mysql, MYSQL_OPT_RECONNECT, &reconnect);
If the connection has gone down, the effect of
mysql_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, 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 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.
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 system variables are reinitialized to the values of the corresponding global system variables, including system 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.The association of the client with the Performance Schema
threads
table row that determines connection thread instrumentation is lost. If the client reconnects after a disconnect, the session is associated with a new row in thethreads
table and the thread monitoring state may be different. See Section 26.12.17.5, “The threads Table”.
If reconnection occurs, any SQL statement specified by calling
mysql_options()
with the
MYSQL_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()
.
Traduction non disponible
Le manuel MySQL n'est pas encore traduit en français sur l'infobrol. Seule la version anglaise est disponible pour l'instant.
Document créé le 26/06/2006, dernière modification le 26/10/2018
Source du document imprimé : https://www.gaudry.be/mysql-rf-c-api-auto-reconnect.html
L'infobrol est un site personnel dont le contenu n'engage que moi. Le texte est mis à disposition sous licence CreativeCommons(BY-NC-SA). Plus d'info sur les conditions d'utilisation et sur l'auteur.
Références
Ces références et liens indiquent des documents consultés lors de la rédaction de cette page, ou qui peuvent apporter un complément d'information, mais les auteurs de ces sources ne peuvent être tenus responsables du contenu de cette page.
L'auteur de ce site est seul responsable de la manière dont sont présentés ici les différents concepts, et des libertés qui sont prises avec les ouvrages de référence. N'oubliez pas que vous devez croiser les informations de sources multiples afin de diminuer les risques d'erreurs.