← Back to team overview

maria-discuss team mailing list archive

Re: mariadb-connector-cpp and multiple threads

 

Lawrin,
 
Yes that was I! Thank you for your response and opening the ticket to track the issue. I will keep an eye out for a resolution! :)
 
 
Thanks,
Jason
 
-----Original Message-----
From: "Lawrin Novitsky" <lawrin.novitsky@xxxxxxxxxxx>
Sent: Monday, July 25, 2022 3:58am
To: "jason@xxxxxxxxxxxxx" <jason@xxxxxxxxxxxxx>, "maria-discuss@xxxxxxxxxxxxxxxxxxx" <maria-discuss@xxxxxxxxxxxxxxxxxxx>
Subject: RE: [Maria-discuss] mariadb-connector-cpp and multiple threads




Hi,
 
I think you also asked this question on stackoverflow. At least the test code looks identical.
The problem looks to be at our side. I’ll open a ticket in our jira for your problem. Or you can do that yourself, if you want.
 
Best regards,
Lawrin
 

From: [ jason@xxxxxxxxxxxxx ]( mailto:jason@xxxxxxxxxxxxx )
Sent: Sunday, July 24, 2022 4:25 AM
To: [ maria-discuss@xxxxxxxxxxxxxxxxxxx ]( mailto:maria-discuss@xxxxxxxxxxxxxxxxxxx )
Subject: [Maria-discuss] mariadb-connector-cpp and multiple threads
 


Using the [ MariaDB C++ Connector ]( https://github.com/mariadb-corporation/mariadb-connector-cpp ) on ubuntu 20.04, and gcc compiler. During load test of a webserver application it was discovered that anytime there was more than a single concurrent connection the server would crash. This was narrowed down to the connect member of the mariadb driver. The errors that were being reported were always one of the following two:
 
terminate called after throwing an instance of 'std::logic_error' what(): basic_string::_M_construct null not valid Aborted (core dumped) terminate called after throwing an instance of 'std::invalid_argument' what(): stoi Aborted (core dumped)


As it wouldn't be feasible to post the source of the server, I have reproduced a minimal working example that uses only standard library and mariadb/conncpp.hpp:

```
#include <mariadb/conncpp.hpp> #include <iostream> #include <thread> #include <string> sql::Driver* driver = sql::mariadb::get_driver_instance(); sql::SQLString connectionUrl = "jdbc:mariadb://localhost:3306/the_database_name"; sql::Properties properties = {{"user", "the_username"}, {"password", "the_password"}}; int main() { // simulate 2 concurrent connections each making 500 requests std::vector<std::function<void()>> t1cbs; std::vector<std::function<void()>> t2cbs; for(int i = 0; i < 500; i++) // if does not fail for you at 500 up the number until it does { t1cbs.push_back([&]{ std::unique_ptr<sql::Connection> conn(driver->connect(connectionUrl, properties)); std::cout << "t1:" << conn->getHostname().c_str() << std::endl; }); // comment out this block to keep the second thread from executing, and you will see // that no errors occur t2cbs.push_back([&]{ std::unique_ptr<sql::Connection> conn(driver->connect(connectionUrl, properties)); std::cout << "t2:" << conn->getHostname().c_str() << std::endl; }); } std::thread t1([&]{ for(auto& cb : t1cbs) cb(); }); std::thread t2([&]{ for(auto& cb : t2cbs) cb(); }); t1.join(); t2.join(); return 0; }

```

I'm guessing the issue is on my end as I've just started working with this particular library and have probably missed or misunderstood something.
Would someone be able to point me in the right direction as far as how I might go about creating database connections from within separate threads?
 

References