boostorg / mysql

MySQL C++ client based on Boost.Asio
https://www.boost.org/doc/libs/master/libs/mysql
Boost Software License 1.0
257 stars 33 forks source link

End of file [asio.misc:2] & Mismatched sequence numbers [mysql.client:5] #162

Closed kylindai closed 1 year ago

kylindai commented 1 year ago

When I update my table in multi-thread way, I always get the following error msgs:

_mysql error_code = End of file [asio.misc:2] mysql errorcode = Mismatched sequence numbers [mysql.client:5]

And I have closed the statement by invoke conn.close_statement() after each calling conn.execute(stmt.bind(...), dig, ec);

Some advice could give me? Thanks a lot~

anarthal commented 1 year ago

Hi @kylindai,

This looks like a race condition on a connection object. By design, connections are not thread-safe, and impose the condition on having a single outstanding network operation at a time.

Some questions to help you move forward:

Regards, Ruben.

kylindai commented 1 year ago

Thanks for getting your reply so soon.

Yes, it's an usual connection pool implement, pre-creating a group of connections in a vector, and each conn has it's own io_context, and pre-connect the mysql server, when the operation finished, not close the connection really.

Is this a wrong usage? Any idea for creating connection pool of using this library? Thanks a lot~

anarthal commented 1 year ago

Hi @kylindai,

The pattern you describe is a valid usage of the library, as long as you can guarantee that only one operation is outstanding for a given connection at a single time. I've got the feeling that your code is failing to guarantee this condition.

If you could somehow share me your connection pool code, I can assist you to find whether this is the case or not.

Edit: are you using sync or async functions?

Regards, Ruben.

kylindai commented 1 year ago

Hi @anarthal

Thanks for your reply, I have got my mistake, it is a bug in my codes:

T& get_conn() { std::lockguard lock(lock);
return conn_holder->get(); // here, many threads could get the same conn that not been released }

bool release(T& conn) { std::lockguard lock(lock); return conn_holder->release(conn); }

The library is excellent !!! This issue could be closed, thanks.

anarthal commented 1 year ago

Glad you could find it. I'm closing this issue now. If you have any further questions, please ask.