I’m using Boost.MySQL library from the boost packages for c++. I have encountered an error when creating and destroying a database connection using local io_context and ssl_contexts.
Please see the minimal reproducible example below –
class DatabaseProcess : public MainframeComponent
{
public:
// MainframeComponent dispatch module type is set to Main because DatabaseProcess doesn't run on its own thread.
DatabaseProcess() : MainframeComponent(Horizon::System::RUNTIME_DATABASE) { }
~DatabaseProcess()
{
}
void initialize(int segment_number = 1) override { HLog(error) << "Database not configured"; }
void initialize(int segment_number, std::string host, int port, std::string user, std::string pass, std::string database);
void finalize(int segment_number = 1) override
{
// Close connection object.
if (_connection != nullptr) {
_connection->close();
}
bool value = _is_initialized;
_is_initialized.compare_exchange_strong(value, false);
}
std::shared_ptr<boost::mysql::tcp_ssl_connection> get_connection() { return _connection; }
bool is_initialized() { return _is_initialized.load(); }
protected:
std::shared_ptr<boost::asio::ssl::context> _ssl_ctx{nullptr};
std::shared_ptr<boost::mysql::tcp_ssl_connection> _connection{nullptr};
boost::asio::io_context _db_io_context;
std::atomic<bool> _is_initialized;
};