← Back to team overview

maria-discuss team mailing list archive

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 validAborted (core dumped)terminate called after throwing an instance of 'std::invalid_argument'  what():  stoiAborted (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?

Follow ups