mirror of
https://github.com/pocoproject/poco.git
synced 2025-10-26 02:18:04 +01:00
move mysql init to createSession() and protect it with mutex
This commit is contained in:
@@ -30,6 +30,7 @@ namespace MySQL {
|
||||
|
||||
|
||||
std::string Connector::KEY(POCO_DATA_MYSQL_CONNECTOR_NAME);
|
||||
Poco::Mutex Connector::_mutex;
|
||||
|
||||
|
||||
Connector::Connector()
|
||||
@@ -50,17 +51,25 @@ const std::string& Connector::name() const
|
||||
Poco::AutoPtr<Poco::Data::SessionImpl> Connector::createSession(const std::string& connectionString,
|
||||
std::size_t timeout)
|
||||
{
|
||||
static bool initDone = false;
|
||||
{
|
||||
Poco::Mutex::ScopedLock l(_mutex);
|
||||
if (!initDone)
|
||||
{
|
||||
if (mysql_library_init(0, 0, 0) != 0)
|
||||
{
|
||||
throw Exception("mysql_library_init error");
|
||||
}
|
||||
initDone = true;
|
||||
}
|
||||
}
|
||||
|
||||
return Poco::AutoPtr<Poco::Data::SessionImpl>(new SessionImpl(connectionString, timeout));
|
||||
}
|
||||
|
||||
|
||||
void Connector::registerConnector()
|
||||
{
|
||||
if (mysql_library_init(0, 0, 0) != 0)
|
||||
{
|
||||
throw Exception("mysql_library_init error");
|
||||
}
|
||||
|
||||
Poco::Data::SessionFactory::instance().add(new Connector());
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user