Constructing a MySQL Connector/C++ class object after one is destroyed
Posted by:
David Hale
Date: November 06, 2023 07:01PM
Using MySQL Connector/C++ 8.0 I define a Database class and construct an object of that class to connect to the database. That works fine. If that object is destroyed and I try to construct another one, then it fails.
Here's the class:
class Database {
private:
std::unique_ptr<mysqlx::Session> _session;
std::unique_ptr<mysqlx::Table> _table;
std::unique_ptr<mysqlx::Schema> _schema;
public:
Database( std::string host,
int port,
std::string user,
std::string pass,
std::string schema,
std::string table ) {
_session = std::make_unique<mysqlx::Session> ( mysqlx::SessionOption::HOST, host,
mysqlx::SessionOption::PORT, port,
mysqlx::SessionOption::USER, user,
mysqlx::SessionOption::PWD, pass );
_schema = std::make_unique<mysqlx::Schema>( *_session, schema );
_table = std::make_unique<mysqlx::Table>( _schema->getTable( table ) );
void write( std::map<std::string, std::string> data ); // details not pertinent here
// close might be overkill? shouldn't things get cleaned up when
// the object goes out of scope and is deconstructed?
//
inline void close() { _session->close(); _session.reset(); _table.reset(); _schema.reset(); }
}
}
I can instantiate a Database object in a thread (only one such "database" thread can ever be started at a time, so there is never any risk of contention), such as:
void SomeClass::dothread_db( host,port,... etc.. ) {
try {
Database::Database database(host,port,user,pass,schema,table);
while (condition ) {
database.write(...);
// things can change the condition
}
database.close(); // is this even needed?
} catch etc..
return;
}
I can spawn dothread_db once, and it connects to the database and writes to it all day long, but if the conditions are met such that it completes, and then I try to spawn another one, I will get one of two possible errors:
CDK Error: OpenSSL: error:00000000:lib(0):func(0):reason(0)
or
CDK Error: Bad file descriptor (generic:9)
when it tries to construct the Database object. I see no messages in /var/log/mysql/ logs.