Skip to content

Commit 2f868bf

Browse files
committed
Merge branch 'bug#22330736' into connector-cpp-1.1.7-release
2 parents 5217078 + 11c6d8e commit 2f868bf

File tree

5 files changed

+147
-51
lines changed

5 files changed

+147
-51
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)
151+
SET(MYSQLCPPCONN_BOOST_COMPONENTS thread date_time system)
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: 57 additions & 37 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>
3132

3233
// Looks like this one should go after private_iface
3334
#include "mysql_driver.h"
3435
#include "nativeapi/native_driver_wrapper.h"
3536

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,48 +67,68 @@ 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+
7088
CPPCONN_PUBLIC_FUNC sql::mysql::MySQL_Driver * get_driver_instance()
7189
{
72-
return get_driver_instance_by_name("");
90+
return get_driver_instance_by_name("");
7391
}
7492

7593

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

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

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

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

90-
return newDriver.get();
91-
}
110+
return newDriver.get();
111+
}
92112
}
93113

94114

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

104124

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

114134

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

119139

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

127147

128148
sql::Connection * MySQL_Driver::connect(sql::ConnectOptionsMap & properties)
129149
{
130-
return new MySQL_Connection(this, proxy->conn_init(),properties);
150+
return new MySQL_Connection(this, proxy->conn_init(),properties);
131151
}
132152

133153

134154
//TODO: That has to be defined in cmake files
135155
int MySQL_Driver::getMajorVersion()
136156
{
137-
return MYCPPCONN_MAJOR_VERSION;
157+
return MYCPPCONN_MAJOR_VERSION;
138158
}
139159

140160
int MySQL_Driver::getMinorVersion()
141161
{
142-
return MYCPPCONN_MINOR_VERSION;
162+
return MYCPPCONN_MINOR_VERSION;
143163
}
144164

145165
int MySQL_Driver::getPatchVersion()
146166
{
147-
return MYCPPCONN_PATCH_VERSION;
167+
return MYCPPCONN_PATCH_VERSION;
148168
}
149169

150170
const sql::SQLString & MySQL_Driver::getName()
151171
{
152-
static const sql::SQLString name("MySQL Connector C++ (libmysql)");
153-
return name;
172+
static const sql::SQLString name("MySQL Connector C++ (libmysql)");
173+
return name;
154174
}
155175

156176

157177
void MySQL_Driver::threadInit()
158178
{
159-
proxy->thread_init();
179+
proxy->thread_init();
160180
}
161181

162182

163183
void MySQL_Driver::threadEnd()
164184
{
165-
proxy->thread_end();
185+
proxy->thread_end();
166186
}
167187

168188
} /* namespace mysql */

test/unit/classes/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ 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} ${MY_GCOV_LINK_LIBRARIES})
51+
TARGET_LINK_LIBRARIES(test_connection ${MY_TARGET_LINK_LIBRARIES}
52+
${MY_GCOV_LINK_LIBRARIES}
53+
${MYSQLCPPCONN_BOOST_THREAD_LIBS} )
5254

5355
MESSAGE(STATUS "Configuring unit tests - connection")
5456

test/unit/classes/connection.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ 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>
3738
#include <list>
3839

3940
namespace testsuite
@@ -2955,6 +2956,69 @@ void connection::reconnect()
29552956
}
29562957
}
29572958

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+
29583022

29593023
} /* namespace connection */
29603024
} /* namespace testsuite */

test/unit/classes/connection.h

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,17 @@ 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);
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);
7980
}
8081

8182
/**
@@ -232,6 +233,15 @@ class connection : public unit_fixture
232233
*
233234
*/
234235
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+
235245
};
236246

237247

0 commit comments

Comments
 (0)