mysql: conn_pool async_execute not found result

#include <iostream>

#include "boost/asio.hpp"
#include "boost/mysql/connection_pool.hpp"

int main() {
    boost::asio::io_context ctx{};
    boost::mysql::pool_params params{};
    params.server_address.emplace_host_and_port("localhost");
    params.username = "root";
    params.password = "123456";
    params.database = "anubis";
    static auto conn_pool = std::make_shared<boost::mysql::connection_pool>(
        boost::mysql::pool_executor_params::thread_safe(ctx.get_executor()), std::move(params));
    if (!conn_pool->valid()) {
        return 1;
    }

    conn_pool->async_run(boost::asio::detached);

    conn_pool->async_get_connection([&](boost::mysql::error_code ec, boost::mysql::pooled_connection conn) {
        if (ec) {
            return;
        }

        // query result ok
        boost::mysql::diagnostics _errorInfo{};
        boost::mysql::results _results{};
        conn->execute("SELECT COUNT(*) FROM `character`;", _results, ec, _errorInfo);
        if (ec) {
            return;
        }

        for (auto rows : _results.rows()) {
            for (std::size_t i = 0; i < rows.size(); ++i) {
                auto field = rows[i];
                std::cout << "execute:" << field.as_int64() << std::endl;
            }
        }

        // query not result
        static boost::mysql::diagnostics _errorInfo2{};
        static boost::mysql::results _results2{};
        conn->async_execute("SELECT COUNT(*) FROM `character`;", _results2, _errorInfo2,
                            [&](boost::mysql::error_code ec) {
                                if (ec) {
                                    return;
                                }

                                // this rows is 0
                                for (auto rows : _results2.rows()) {
                                    for (std::size_t i = 0; i < rows.size(); ++i) {
                                        auto field = rows[i];
                                        std::cout << "async_execute:" << field.as_int64() << std::endl;
                                    }
                                }
                            });
    });

    ctx.run();
    return 0;
}

image

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 16

Most upvoted comments

Destroying a pooled_connection is how you tell the pool that you’re done using the connection and it can be recycled.

Yes, I understand. I used to think that boost:: mysql:: pooled_connection was managed internally.

If you’re going to use callbacks, I’d advise to create a session object on the heap, storing all intermediate state that must be kept alive until the operation completes. For instance:


class session : public std::enable_shared_from_this<session> {
    boost::mysql::connection_pool& pool_;
    boost::mysql::pooled_connection conn_;
    boost::mysql::diagnostics error_info_;
    boost::mysql::results results_;

    void print_results()
    {
        std::cout << "step 3 row_size:" << results.rows().size() << std::endl;
        for (auto rows : results.rows()) {
            for (std::size_t i = 0; i < rows.size(); ++i) {
                auto field = rows[i];
                std::cout << "async_execute:" << field.as_int64() << std::endl;
            }
        }
    }
public:
    session() = default;

    void start(boost::mysql::connection_pool& pool)
    {
        pool.async_get_connection(
            std::chrono::seconds(0), [self = this->shared_from_this()](boost::mysql::error_code ec, boost::mysql::pooled_connection c) {
                self->conn = std::move(c);
                std::cout << "step 1" << std::endl;
                if (ec) {
                    std::cout << "Error getting connection: " << ec << std::endl;
                    return;
                }
                self->execute();
            }
        );
    }

    void execute()
    {
        conn->async_execute(
            "SELECT COUNT(*) FROM `character`;",
            results_,
            error_info_,
            [self = this->shared_from_this()](boost::mysql::error_code ec) {
                std::cout << "step 2" << std::endl;
                if (ec) {
                    std::cout << "Error executing query: " << ec.value() << ":" << ec.message() << std::endl;
                    return;
                }
                self->print_results();
            }
        );
    }
};

int main() {
    boost::asio::io_context ctx{};
    boost::mysql::pool_params params{};
    params.server_address.emplace_host_and_port("127.0.0.1", 3306);
    params.username = "root";
    params.password = "123456";
    params.database = "boat";
    auto conn_pool = std::make_shared<boost::mysql::connection_pool>(
        boost::mysql::pool_executor_params::thread_safe(ctx.get_executor()), std::move(params));

    conn_pool->async_run(boost::asio::detached);

    auto session = std::make_shared<session>();
    session->start(*conn_pool);
    ctx.run();
    return 0;
}

I haven’t built the code, it may contain typos.

Your conn variable is getting destroyed right after calling conn->execute, which makes the connection to be returned to the pool while you’re still using it, causing undefined behavior. This should work:

#include <iostream>

#include "boost/asio.hpp"
#include "boost/mysql/connection_pool.hpp"

int main() {
    boost::asio::io_context ctx{};
    boost::mysql::pool_params params{};
    params.server_address.emplace_host_and_port("127.0.0.1", 3306);
    params.username = "root";
    params.password = "123456";
    params.database = "boat";
    auto conn_pool = std::make_shared<boost::mysql::connection_pool>(
        boost::mysql::pool_executor_params::thread_safe(ctx.get_executor()), std::move(params));

    conn_pool->async_run(boost::asio::detached);

    std::thread th([&]() {
        ctx.run();
    });

    boost::mysql::diagnostics error_info{};
    boost::mysql::results results{};
    boost::mysql::pooled_connection conn;
    conn_pool->async_get_connection(
        std::chrono::seconds(0), [&](boost::mysql::error_code ec, boost::mysql::pooled_connection c) {
            conn = std::move(c);
            std::cout << "step 1" << std::endl;
            if (ec) {
                std::cout << "Error getting connection: " << ec << std::endl;
                return;
            }

            conn->async_execute(
                "SELECT COUNT(*) FROM `character`;", results, error_info, [&](boost::mysql::error_code ec) {
                    std::cout << "step 2" << std::endl;

                    if (ec) {
                        std::cout << "Error executing query: " << ec.value() << ":" << ec.message() << std::endl;
                        return;
                    }

                    std::cout << "step 3 row_size:" << results.rows().size() << std::endl;
                    for (auto rows : results.rows()) {
                        for (std::size_t i = 0; i < rows.size(); ++i) {
                            auto field = rows[i];
                            std::cout << "async_execute:" << field.as_int64() << std::endl;
                        }
                    }
                });
        });

    std::this_thread::sleep_for(std::chrono::seconds(100));
    ctx.stop();
    th.join();
    return 0;
}