4
4
node_db_mysql::Connection::Connection ()
5
5
: compress(false ),
6
6
readTimeout(0 ),
7
- reconnect(false ),
7
+ reconnect(true ),
8
8
sslVerifyServer(false ),
9
9
timeout(0 ),
10
10
writeTimeout(0 ),
@@ -51,6 +51,13 @@ void node_db_mysql::Connection::setWriteTimeout(const uint32_t writeTimeout) thr
51
51
this ->writeTimeout = writeTimeout;
52
52
}
53
53
54
+ bool node_db_mysql::Connection::isAlive (bool ping) throw() {
55
+ if (ping && this ->alive ) {
56
+ this ->alive = (mysql_ping (this ->connection ) == 0 );
57
+ }
58
+ return this ->alive ;
59
+ }
60
+
54
61
void node_db_mysql::Connection::open () throw(node_db::Exception&) {
55
62
this ->close ();
56
63
@@ -84,7 +91,7 @@ void node_db_mysql::Connection::open() throw(node_db::Exception&) {
84
91
mysql_options (this ->connection , MYSQL_OPT_WRITE_TIMEOUT, (const char *) &this ->writeTimeout );
85
92
}
86
93
87
- this ->opened = mysql_real_connect (
94
+ this ->alive = mysql_real_connect (
88
95
this ->connection ,
89
96
this ->hostname .c_str (),
90
97
this ->user .c_str (),
@@ -99,16 +106,16 @@ void node_db_mysql::Connection::open() throw(node_db::Exception&) {
99
106
mysql_options (this ->connection , MYSQL_OPT_RECONNECT, (const char *) &this ->reconnect );
100
107
#endif
101
108
102
- if (!this ->opened ) {
109
+ if (!this ->alive ) {
103
110
throw node_db::Exception (mysql_error (this ->connection ));
104
111
}
105
112
}
106
113
107
114
void node_db_mysql::Connection::close () {
108
- if (this ->opened ) {
115
+ if (this ->alive ) {
109
116
mysql_close (this ->connection );
110
117
}
111
- this ->opened = false ;
118
+ this ->alive = false ;
112
119
}
113
120
114
121
std::string node_db_mysql::Connection::escape (const std::string& string) const throw(node_db::Exception&) {
@@ -125,10 +132,7 @@ std::string node_db_mysql::Connection::escape(const std::string& string) const t
125
132
}
126
133
127
134
std::string node_db_mysql::Connection::version () const {
128
- std::string version;
129
- if (this ->opened ) {
130
- version = mysql_get_server_info (this ->connection );
131
- }
135
+ std::string version = mysql_get_server_info (this ->connection );
132
136
return version;
133
137
}
134
138
@@ -137,10 +141,6 @@ node_db::Result* node_db_mysql::Connection::query(const std::string& query) cons
137
141
throw node_db::Exception (" This binding needs to be linked with the thread safe MySQL library libmysqlclient_r" );
138
142
#endif
139
143
140
- if (!this ->opened ) {
141
- throw node_db::Exception (" Can't execute query without an opened connection" );
142
- }
143
-
144
144
if (mysql_real_query (this ->connection , query.c_str (), query.length ()) != 0 ) {
145
145
throw node_db::Exception (mysql_error (this ->connection ));
146
146
}
0 commit comments