diff --git a/example/metadata.cpp b/example/metadata.cpp index 0409de44..056752f6 100644 --- a/example/metadata.cpp +++ b/example/metadata.cpp @@ -78,6 +78,9 @@ void main_impl(int argc, char** argv) assert(employee_id.is_primary_key()); // field is a primary key assert(employee_id.is_auto_increment()); // we declared the field as AUTO_INCREMENT assert(employee_id.is_not_null()); // field cannot be NULL + + // Close the connection + conn.close(); } int main(int argc, char** argv) diff --git a/example/prepared_statements.cpp b/example/prepared_statements.cpp index 78118f2e..18f51520 100644 --- a/example/prepared_statements.cpp +++ b/example/prepared_statements.cpp @@ -130,6 +130,9 @@ void main_impl(int argc, char** argv) */ salary_updater.close(); salary_getter.close(); + + // Close the connection + conn.close(); } int main(int argc, char** argv) diff --git a/example/query_async_callbacks.cpp b/example/query_async_callbacks.cpp index 71ed28f9..971da8d4 100644 --- a/example/query_async_callbacks.cpp +++ b/example/query_async_callbacks.cpp @@ -134,10 +134,19 @@ public: assert(rows.size() == 1); [[maybe_unused]] auto salary = std::get(rows[0].values()[0]); assert(salary == 15000); + close(); }, &additional_info); }, &additional_info); } + void close() + { + // Notify the MySQL server we want to quit and then close the socket + connection.async_close([this](error_code err) { + die_on_error(err, additional_info); + }, &additional_info); + } + auto& context() { return ctx; } }; diff --git a/example/query_async_coroutines.cpp b/example/query_async_coroutines.cpp index de72f45d..2655a26d 100644 --- a/example/query_async_coroutines.cpp +++ b/example/query_async_coroutines.cpp @@ -135,6 +135,10 @@ void main_impl(int argc, char** argv) if (!row) break; // No more rows available print_employee(*row); } + + // Notify the MySQL server we want to quit, then close the underlying connection. + conn.async_close(yield[ec], &additional_info); + check_error(ec, additional_info); }); // Don't forget to call run()! Otherwise, your program diff --git a/example/query_async_futures.cpp b/example/query_async_futures.cpp index cf865020..b51faaa4 100644 --- a/example/query_async_futures.cpp +++ b/example/query_async_futures.cpp @@ -132,6 +132,9 @@ void main_impl(int argc, char** argv) print_employee(*current_row); } + // Notify the MySQL server we want to quit, then close the underlying connection. + conn.async_close(use_future).get(); + // application dtor. stops io_context and then joins the thread } diff --git a/example/query_sync.cpp b/example/query_sync.cpp index 4e076aed..7ac907ae 100644 --- a/example/query_sync.cpp +++ b/example/query_sync.cpp @@ -111,6 +111,11 @@ void main_impl(int argc, char** argv) assert(rows.size() == 1); [[maybe_unused]] auto salary = std::get(rows[0].values()[0]); assert(salary == 10000); + + // Close the connection. This notifies the MySQL we want to log out + // and then closes the underlying socket. This operation implies a network + // transfer and thus can fail + conn.close(); } int main(int argc, char** argv) diff --git a/example/unix_socket.cpp b/example/unix_socket.cpp index a902d56b..146de1b7 100644 --- a/example/unix_socket.cpp +++ b/example/unix_socket.cpp @@ -95,6 +95,9 @@ void main_impl(int argc, char** argv) assert(rows.size() == 1); [[maybe_unused]] auto salary = std::get(rows[0].values()[0]); assert(salary == 10000); + + // Notify the MySQL server we want to quit, then close the underlying connection. + conn.close(); } #else