move mysql init to createSession() and protect it with mutex

This commit is contained in:
Alex Fabijanic
2017-06-22 22:42:21 +02:00
parent 2fd58d54bc
commit 8aecf8a9b0
4 changed files with 245 additions and 226 deletions

View File

@@ -22,6 +22,7 @@
#include "Poco/Data/MySQL/MySQL.h" #include "Poco/Data/MySQL/MySQL.h"
#include "Poco/Data/Connector.h" #include "Poco/Data/Connector.h"
#include "Poco/Mutex.h"
// Note: to avoid static (de)initialization problems, // Note: to avoid static (de)initialization problems,
@@ -60,6 +61,8 @@ public:
static void unregisterConnector(); static void unregisterConnector();
/// Unregisters the Connector under the Keyword Connector::KEY at the Poco::Data::SessionFactory /// Unregisters the Connector under the Keyword Connector::KEY at the Poco::Data::SessionFactory
static Poco::Mutex _mutex;
}; };

View File

@@ -30,6 +30,7 @@ namespace MySQL {
std::string Connector::KEY(POCO_DATA_MYSQL_CONNECTOR_NAME); std::string Connector::KEY(POCO_DATA_MYSQL_CONNECTOR_NAME);
Poco::Mutex Connector::_mutex;
Connector::Connector() Connector::Connector()
@@ -50,17 +51,25 @@ const std::string& Connector::name() const
Poco::AutoPtr<Poco::Data::SessionImpl> Connector::createSession(const std::string& connectionString, Poco::AutoPtr<Poco::Data::SessionImpl> Connector::createSession(const std::string& connectionString,
std::size_t timeout) 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)); return Poco::AutoPtr<Poco::Data::SessionImpl>(new SessionImpl(connectionString, timeout));
} }
void Connector::registerConnector() void Connector::registerConnector()
{ {
if (mysql_library_init(0, 0, 0) != 0)
{
throw Exception("mysql_library_init error");
}
Poco::Data::SessionFactory::instance().add(new Connector()); Poco::Data::SessionFactory::instance().add(new Connector());
} }

View File

@@ -49,16 +49,22 @@ Poco::SharedPtr<SQLExecutor> MySQLTest::_pExecutor = 0;
std::string MySQLTest::getHost() std::string MySQLTest::getHost()
{ {
return "localhost"; return "127.0.0.1"; //localhost";
} }
std::string MySQLTest::getPort() std::string MySQLTest::getPort()
{ {
return "3306"; return "3306";
} }
std::string MySQLTest::getUser() std::string MySQLTest::getUser()
{ {
return "root"; return "root";
} }
std::string MySQLTest::getPass() std::string MySQLTest::getPass()
{ {
if (Environment::has("APPVEYOR")) if (Environment::has("APPVEYOR"))
@@ -68,18 +74,20 @@ std::string MySQLTest::getPass()
else else
return "poco"; return "poco";
} }
std::string MySQLTest::getBase() std::string MySQLTest::getBase()
{ {
return "pocotestdb"; return "pocotestdb";
} }
std::string MySQLTest::_dbConnString;
std::string MySQLTest::_dbConnString;
// //
// Connection string // Connection string
//
MySQLTest::MySQLTest(const std::string& name): MySQLTest::MySQLTest(const std::string& name):
CppUnit::TestCase(name) CppUnit::TestCase(name)

File diff suppressed because it is too large Load Diff