Closed sagunkho closed 4 months ago
The io_context
and ssl::context
objects must be kept alive as long as you're using the connection. Try this:
struct connection_block {
boost::asio::io_context ctx;
boost::asio::ssl::context ssl_ctx {boost::asio::ssl::context::tls_client};
boost::mysql::tcp_ssl_connection conn {ctx.get_executor(), ssl_ctx};
};
void initialize(std::string host, int port, std::string user, std::string pass, std::string database)
{
try {
_connection = std::make_shared<connection_block>();
boost::asio::ip::tcp::resolver resolver(_connection->ctx.get_executor());
auto endpoints = resolver.resolve(host, std::to_string(port));
boost::mysql::handshake_params params(user, pass, database);
_connection->conn.connect(*endpoints.begin(), params);
} catch (boost::mysql::error_with_diagnostics &error) {
HLog(error) << error.what();
}
bool value = _is_initialized;
_is_initialized.compare_exchange_strong(value, true);
}
boost::mysql::tcp_ssl_connection& get_connection() { return _connection->conn; }
@anarthal Thanks, I'm using a global io_context
now and it works well.
That's the intended use. Remember to keep the SSL context alive, too. Recall also that a single io_context and ssl::context can be used to create as many connections as you want.
I'm using Boost.MySQL with boost version 1.84. I want to use the connection so I've created a class and added a connection member variable. I acquire a connection from the MySQL server and save the connection in the class. What happens next is that when I access the connection, I get a file handle error.
The code relevant to the saving of the connection is as follows -
The
_connection
variable is saved in the class and I can later call a small function to test the connection -The issue arises in calling the function. It fails on the
execute
step, but if I perform the execute within the function where the connection was created, it works.