Skip to content

Commit 877b8cb

Browse files
committed
Revert "Bug #22330736 Fix get_driver_instance() to be thread-safe"
This reverts commit 11c6d8e.
1 parent 2f868bf commit 877b8cb

File tree

5 files changed

+51
-147
lines changed

5 files changed

+51
-147
lines changed

CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,10 @@ ENDIF(NOT BOOST_ROOT AND WIN32)
148148
# Prefer static linking in all cases
149149
SET(Boost_ADDITIONAL_VERSIONS "1.37.0" "1.37" "1.38.0" "1.38" "1.39.0" "1.39")
150150
SET(Boost_NO_BOOST_CMAKE ON CACHE BOOL "Enable fix for FindBoost.cmake")
151-
SET(MYSQLCPPCONN_BOOST_COMPONENTS thread date_time system)
151+
SET(MYSQLCPPCONN_BOOST_COMPONENTS thread date_time)
152152
SET(Boost_USE_STATIC_LIBS TRUE)
153-
FIND_PACKAGE(Boost COMPONENTS ${MYSQLCPPCONN_BOOST_COMPONENTS})
154-
#FIND_PACKAGE(Boost)
153+
#FIND_PACKAGE(Boost COMPONENTS ${MYSQLCPPCONN_BOOST_COMPONENTS})
154+
FIND_PACKAGE(Boost)
155155
IF(NOT Boost_FOUND)
156156
# Try dynamic
157157
SET(Boost_USE_STATIC_LIBS FALSE)

driver/mysql_driver.cpp

Lines changed: 37 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -28,31 +28,31 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2828
#include "version_info.h"
2929

3030
#include <cppconn/exception.h>
31-
#include <boost/thread/mutex.hpp>
3231

3332
// Looks like this one should go after private_iface
3433
#include "mysql_driver.h"
3534
#include "nativeapi/native_driver_wrapper.h"
3635

36+
3737
extern "C"
3838
{
3939
CPPCONN_PUBLIC_FUNC void * sql_mysql_get_driver_instance()
4040
{
41-
void * ret = sql::mysql::get_driver_instance();
42-
return ret;
41+
void * ret = sql::mysql::get_driver_instance();
42+
return ret;
4343
}
4444

4545

4646
/* these are the functions without namespace - from cppconn/driver.h */
4747
CPPCONN_PUBLIC_FUNC sql::Driver * get_driver_instance_by_name(const char * const clientlib)
4848
{
49-
return sql::mysql::get_driver_instance_by_name(clientlib);
49+
return sql::mysql::get_driver_instance_by_name(clientlib);
5050
}
5151

5252

5353
CPPCONN_PUBLIC_FUNC sql::Driver * get_driver_instance()
5454
{
55-
return sql::mysql::get_driver_instance();
55+
return sql::mysql::get_driver_instance();
5656
}
5757

5858

@@ -67,68 +67,48 @@ static const ::sql::SQLString emptyStr("");
6767
/* Mapping by client name is probably not enough here */
6868
static std::map< sql::SQLString, boost::shared_ptr<MySQL_Driver> > driver;
6969

70-
class Driver_mutex
71-
{
72-
static boost::mutex mtx;
73-
74-
public:
75-
Driver_mutex()
76-
{
77-
mtx.lock();
78-
}
79-
~Driver_mutex()
80-
{
81-
mtx.unlock();
82-
}
83-
};
84-
85-
boost::mutex Driver_mutex::mtx;
86-
87-
8870
CPPCONN_PUBLIC_FUNC sql::mysql::MySQL_Driver * get_driver_instance()
8971
{
90-
return get_driver_instance_by_name("");
72+
return get_driver_instance_by_name("");
9173
}
9274

9375

9476
CPPCONN_PUBLIC_FUNC sql::mysql::MySQL_Driver * get_driver_instance_by_name(const char * const clientlib)
9577
{
96-
::sql::SQLString dummy(clientlib);
97-
98-
std::map< sql::SQLString, boost::shared_ptr< MySQL_Driver > >::const_iterator cit;
78+
::sql::SQLString dummy(clientlib);
9979

100-
Driver_mutex mtx;
80+
std::map< sql::SQLString, boost::shared_ptr< MySQL_Driver > >::const_iterator cit;
10181

102-
if ((cit = driver.find(dummy)) != driver.end()) {
103-
return cit->second.get();
104-
} else {
105-
boost::shared_ptr< MySQL_Driver > newDriver;
82+
if ((cit = driver.find(dummy)) != driver.end()) {
83+
return cit->second.get();
84+
} else {
85+
boost::shared_ptr< MySQL_Driver > newDriver;
10686

107-
newDriver.reset(new MySQL_Driver(dummy));
108-
driver[dummy] = newDriver;
87+
newDriver.reset(new MySQL_Driver(dummy));
88+
driver[dummy] = newDriver;
10989

110-
return newDriver.get();
111-
}
90+
return newDriver.get();
91+
}
11292
}
11393

11494

11595
MySQL_Driver::MySQL_Driver()
11696
{
117-
try {
118-
proxy.reset(::sql::mysql::NativeAPI::createNativeDriverWrapper(emptyStr));
119-
} catch(std::runtime_error & e) {
120-
throw sql::InvalidArgumentException(e.what());
121-
}
97+
try {
98+
proxy.reset(::sql::mysql::NativeAPI::createNativeDriverWrapper(emptyStr));
99+
} catch(std::runtime_error & e) {
100+
throw sql::InvalidArgumentException(e.what());
101+
}
122102
}
123103

124104

125105
MySQL_Driver::MySQL_Driver(const ::sql::SQLString & clientLib)
126106
{
127-
try {
128-
proxy.reset(::sql::mysql::NativeAPI::createNativeDriverWrapper(clientLib));
129-
} catch(std::runtime_error & e) {
130-
throw sql::InvalidArgumentException(e.what());
131-
}
107+
try {
108+
proxy.reset(::sql::mysql::NativeAPI::createNativeDriverWrapper(clientLib));
109+
} catch(std::runtime_error & e) {
110+
throw sql::InvalidArgumentException(e.what());
111+
}
132112
}
133113

134114

@@ -138,51 +118,51 @@ MySQL_Driver::~MySQL_Driver()
138118

139119

140120
sql::Connection * MySQL_Driver::connect(const sql::SQLString& hostName,
141-
const sql::SQLString& userName,
142-
const sql::SQLString& password)
121+
const sql::SQLString& userName,
122+
const sql::SQLString& password)
143123
{
144-
return new MySQL_Connection(this, proxy->conn_init(),hostName, userName, password);
124+
return new MySQL_Connection(this, proxy->conn_init(),hostName, userName, password);
145125
}
146126

147127

148128
sql::Connection * MySQL_Driver::connect(sql::ConnectOptionsMap & properties)
149129
{
150-
return new MySQL_Connection(this, proxy->conn_init(),properties);
130+
return new MySQL_Connection(this, proxy->conn_init(),properties);
151131
}
152132

153133

154134
//TODO: That has to be defined in cmake files
155135
int MySQL_Driver::getMajorVersion()
156136
{
157-
return MYCPPCONN_MAJOR_VERSION;
137+
return MYCPPCONN_MAJOR_VERSION;
158138
}
159139

160140
int MySQL_Driver::getMinorVersion()
161141
{
162-
return MYCPPCONN_MINOR_VERSION;
142+
return MYCPPCONN_MINOR_VERSION;
163143
}
164144

165145
int MySQL_Driver::getPatchVersion()
166146
{
167-
return MYCPPCONN_PATCH_VERSION;
147+
return MYCPPCONN_PATCH_VERSION;
168148
}
169149

170150
const sql::SQLString & MySQL_Driver::getName()
171151
{
172-
static const sql::SQLString name("MySQL Connector C++ (libmysql)");
173-
return name;
152+
static const sql::SQLString name("MySQL Connector C++ (libmysql)");
153+
return name;
174154
}
175155

176156

177157
void MySQL_Driver::threadInit()
178158
{
179-
proxy->thread_init();
159+
proxy->thread_init();
180160
}
181161

182162

183163
void MySQL_Driver::threadEnd()
184164
{
185-
proxy->thread_end();
165+
proxy->thread_end();
186166
}
187167

188168
} /* namespace mysql */

test/unit/classes/CMakeLists.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,7 @@ SET_TARGET_PROPERTIES(test_connection PROPERTIES
4848
OUTPUT_NAME "connection"
4949
LINK_FLAGS "${MYSQLCPPCONN_LINK_FLAGS_ENV} ${MYSQL_LINK_FLAGS}"
5050
COMPILE_FLAGS "${MYSQLCPPCONN_COMPILE_FLAGS_ENV}")
51-
TARGET_LINK_LIBRARIES(test_connection ${MY_TARGET_LINK_LIBRARIES}
52-
${MY_GCOV_LINK_LIBRARIES}
53-
${MYSQLCPPCONN_BOOST_THREAD_LIBS} )
51+
TARGET_LINK_LIBRARIES(test_connection ${MY_TARGET_LINK_LIBRARIES} ${MY_GCOV_LINK_LIBRARIES})
5452

5553
MESSAGE(STATUS "Configuring unit tests - connection")
5654

test/unit/classes/connection.cpp

Lines changed: 0 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
3434
#include <cppconn/exception.h>
3535

3636
#include <boost/scoped_ptr.hpp>
37-
#include <boost/thread.hpp>
3837
#include <list>
3938

4039
namespace testsuite
@@ -2956,69 +2955,6 @@ void connection::reconnect()
29562955
}
29572956
}
29582957

2959-
void connection::worker_thread()
2960-
{
2961-
try
2962-
{
2963-
sql::Driver *driver;
2964-
sql::Connection *con;
2965-
sql::Statement *stmt;
2966-
sql::ResultSet *res;
2967-
2968-
driver = get_driver_instance_by_name(m_driver_name.c_str());
2969-
2970-
driver->threadInit();
2971-
con = driver->connect(url, user, passwd);
2972-
stmt = con->createStatement();
2973-
res = stmt->executeQuery("SELECT now() as n;");
2974-
while (res->next())
2975-
{
2976-
logMsg(res->getString("n"));
2977-
}
2978-
2979-
delete res;
2980-
delete stmt;
2981-
delete con;
2982-
driver->threadEnd();
2983-
}
2984-
catch (sql::SQLException &e)
2985-
{
2986-
logErr(e.what());
2987-
logErr("SQLState: " + std::string(e.getSQLState()));
2988-
fail(e.what(), __FILE__, __LINE__);
2989-
}
2990-
2991-
return;
2992-
}
2993-
2994-
2995-
void connection::get_driver_multithread()
2996-
{
2997-
const int opt_threads=2;
2998-
2999-
for(int i =0; i < 100; ++i)
3000-
{
3001-
std::stringstream snum;
3002-
snum << i;
3003-
m_driver_name = snum.str();
3004-
3005-
std::vector< boost::shared_ptr<boost::thread> > work_threads;
3006-
3007-
for(unsigned int i=0;i<opt_threads;i++)
3008-
work_threads.push_back(
3009-
boost::shared_ptr<boost::thread>(
3010-
new boost::thread(boost::bind(&connection::worker_thread,this))));
3011-
3012-
std::vector< boost::shared_ptr<boost::thread> >::iterator it = work_threads.begin();
3013-
for(; it != work_threads.end() ;++it)
3014-
{
3015-
(*it)->join();
3016-
}
3017-
}
3018-
3019-
return;
3020-
}
3021-
30222958

30232959
} /* namespace connection */
30242960
} /* namespace testsuite */

test/unit/classes/connection.h

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,16 @@ class connection : public unit_fixture
6666
#ifndef MYSQLCLIENT_STATIC_BINDING
6767
TEST_CASE(loadSameLibraryTwice);
6868
#endif
69-
TEST_CASE(enableClearTextAuth);
70-
TEST_CASE(connectAttrAdd);
71-
TEST_CASE(connectAttrReset);
72-
TEST_CASE(connectCharsetDir);
73-
TEST_CASE(connectSSLEnforce);
74-
TEST_CASE(setAuthDir);
75-
TEST_CASE(setDefaultAuth);
76-
TEST_CASE(localInfile);
77-
TEST_CASE(isValid);
78-
TEST_CASE(reconnect);
79-
TEST_CASE(get_driver_multithread);
69+
TEST_CASE(enableClearTextAuth);
70+
TEST_CASE(connectAttrAdd);
71+
TEST_CASE(connectAttrReset);
72+
TEST_CASE(connectCharsetDir);
73+
TEST_CASE(connectSSLEnforce);
74+
TEST_CASE(setAuthDir);
75+
TEST_CASE(setDefaultAuth);
76+
TEST_CASE(localInfile);
77+
TEST_CASE(isValid);
78+
TEST_CASE(reconnect);
8079
}
8180

8281
/**
@@ -233,15 +232,6 @@ class connection : public unit_fixture
233232
*
234233
*/
235234
void reconnect();
236-
237-
/*
238-
* Test of mysql_get_driver() multithread
239-
*
240-
*/
241-
std::string m_driver_name;
242-
void worker_thread();
243-
void get_driver_multithread();
244-
245235
};
246236

247237

0 commit comments

Comments
 (0)