Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

Reconnecting a MySQL connection
PrevUpHomeNext

After you close a connection or an error has occurred, and if its underlying Stream supports it, you can re-open an existing connection. This is the case for tcp_connection and unix_connection.

[Warning] Warning

Unfortunately, boost::asio::ssl::stream does not support reconnection. If you are using tcp_ssl_connection and you close the connection or encounter an error, you will have to destroy and re-create the connection object.

If you are using tcp_connection or unix_connection, or any other stream supporting reconnection:

If your Stream type doesn't fulfill the SocketStream type requirements, then you can't use connection::connect or connection::close, and you are thus responsible for establishing the physical connection and closing the underlying stream, if necessary. Some guidelines:

  • After calling connection::quit, you should close the underlying stream, if required. You should then re-establish the physical connection on the stream, and call connection::handshake afterwards.
  • If your connection::handshake operation failed, you are responsible for closing the underlying stream if required. You should then establish the physical connection again, and then call connection::handshake.
  • If you connected your connection successfully but encountered a network problem in any subsequent operation, and you would like to re-establish connection, you should call connection::quit first, then close and re-open the physical connection, and finally call connection::handshake.

Note that Boost.MySQL does not perform any built-in retry strategy, as different use cases have different requirements. You can implement it as you best like with these tools. If you implemented your own and you would like to contribute it, please create a PR in the GitHub repository.


PrevUpHomeNext