mirror of
https://github.com/pocoproject/poco.git
synced 2025-10-27 02:53:10 +01:00
move mysql init to createSession() and protect it with mutex
This commit is contained in:
@@ -22,6 +22,7 @@
|
||||
|
||||
#include "Poco/Data/MySQL/MySQL.h"
|
||||
#include "Poco/Data/Connector.h"
|
||||
#include "Poco/Mutex.h"
|
||||
|
||||
|
||||
// Note: to avoid static (de)initialization problems,
|
||||
@@ -60,6 +61,8 @@ public:
|
||||
|
||||
static void unregisterConnector();
|
||||
/// Unregisters the Connector under the Keyword Connector::KEY at the Poco::Data::SessionFactory
|
||||
|
||||
static Poco::Mutex _mutex;
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
|
||||
@@ -49,16 +49,22 @@ Poco::SharedPtr<SQLExecutor> MySQLTest::_pExecutor = 0;
|
||||
|
||||
std::string MySQLTest::getHost()
|
||||
{
|
||||
return "localhost";
|
||||
return "127.0.0.1"; //localhost";
|
||||
}
|
||||
|
||||
|
||||
std::string MySQLTest::getPort()
|
||||
{
|
||||
return "3306";
|
||||
}
|
||||
|
||||
|
||||
std::string MySQLTest::getUser()
|
||||
{
|
||||
return "root";
|
||||
}
|
||||
|
||||
|
||||
std::string MySQLTest::getPass()
|
||||
{
|
||||
if (Environment::has("APPVEYOR"))
|
||||
@@ -68,18 +74,20 @@ std::string MySQLTest::getPass()
|
||||
else
|
||||
return "poco";
|
||||
}
|
||||
|
||||
|
||||
std::string MySQLTest::getBase()
|
||||
{
|
||||
return "pocotestdb";
|
||||
}
|
||||
|
||||
std::string MySQLTest::_dbConnString;
|
||||
|
||||
std::string MySQLTest::_dbConnString;
|
||||
|
||||
|
||||
//
|
||||
// Connection string
|
||||
|
||||
//
|
||||
|
||||
MySQLTest::MySQLTest(const std::string& name):
|
||||
CppUnit::TestCase(name)
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user