diff --git a/sql-odbc/src/sqlodbc/opensearch_communication.cpp b/sql-odbc/src/sqlodbc/opensearch_communication.cpp index 66ca0e03a6..8fdfcae4f2 100644 --- a/sql-odbc/src/sqlodbc/opensearch_communication.cpp +++ b/sql-odbc/src/sqlodbc/opensearch_communication.cpp @@ -32,6 +32,8 @@ // clang-format off #include "opensearch_odbc.h" #include "mylog.h" +#include +#include #include #include #include @@ -121,6 +123,40 @@ static const std::string ERROR_RESPONSE_SCHEMA = R"EOF( } )EOF"; +namespace { + /** + * A helper class to initialize/shutdown AWS API once per DLL load/unload. + */ + class AwsSdkHelper { + public: + AwsSdkHelper() : + m_reference_count(0) { + } + + AwsSdkHelper& operator++() { + if (1 == ++m_reference_count) { + std::scoped_lock lock(m_mutex); + Aws::InitAPI(m_sdk_options); + } + return *this; + } + + AwsSdkHelper& operator--() { + if (0 == --m_reference_count) { + std::scoped_lock lock(m_mutex); + Aws::ShutdownAPI(m_sdk_options); + } + return *this; + } + + Aws::SDKOptions m_sdk_options; + std::atomic m_reference_count; + std::mutex m_mutex; + }; + + AwsSdkHelper AWS_SDK_HELPER; +} + void OpenSearchCommunication::AwsHttpResponseToString( std::shared_ptr< Aws::Http::HttpResponse > response, std::string& output) { // This function has some unconventional stream operations because we need @@ -237,13 +273,11 @@ OpenSearchCommunication::OpenSearchCommunication() #pragma clang diagnostic pop #endif // __APPLE__ { - LogMsg(OPENSEARCH_ALL, "Initializing Aws API."); - Aws::InitAPI(m_options); + ++AWS_SDK_HELPER; } OpenSearchCommunication::~OpenSearchCommunication() { - LogMsg(OPENSEARCH_ALL, "Shutting down Aws API."); - Aws::ShutdownAPI(m_options); + --AWS_SDK_HELPER; } std::string OpenSearchCommunication::GetErrorMessage() { diff --git a/sql-odbc/src/sqlodbc/opensearch_communication.h b/sql-odbc/src/sqlodbc/opensearch_communication.h index 6c141bfe08..4a328ece3b 100644 --- a/sql-odbc/src/sqlodbc/opensearch_communication.h +++ b/sql-odbc/src/sqlodbc/opensearch_communication.h @@ -116,7 +116,6 @@ class OpenSearchCommunication { OpenSearchResultQueue m_result_queue; runtime_options m_rt_opts; std::string m_client_encoding; - Aws::SDKOptions m_options; std::string m_response_str; std::shared_ptr< Aws::Http::HttpClient > m_http_client; std::string m_error_message_to_user;