Skip to content

Commit 587456a

Browse files
committed
Enabling reconnection by default and Database.isConnected() properly pings the server to check status of connection
1 parent f6cd3d7 commit 587456a

File tree

6 files changed

+22
-16
lines changed

6 files changed

+22
-16
lines changed

CHANGELOG

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ For more in depth changelog, check the Github Commits:
22

33
https://github.com/mariano/node-db-mysql/commits/master
44

5+
0.6.2
6+
-----
7+
* Reconnection is enabled by default
8+
* Database.isConnected() properly pings the server to check status of connection
9+
510
0.6.1
611
-----
712
* Fixing issue where Query.execute(callback, options) was not easily doable

lib/node-db

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
, "description" : "MySQL database bindings for Node.JS"
33
, "keywords": ["database","db","native","binding","library","plugin","client","mysql","libmysql"]
44
, "homepage" : "http://nodejsdb.org/db-mysql"
5-
, "version" : "0.6.1"
5+
, "version" : "0.6.2"
66
, "engines" : { "node" : ">=0.4.1" }
77
, "maintainers" :
88
[ { "name": "Mariano Iglesias"

src/connection.cc

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
node_db_mysql::Connection::Connection()
55
: compress(false),
66
readTimeout(0),
7-
reconnect(false),
7+
reconnect(true),
88
sslVerifyServer(false),
99
timeout(0),
1010
writeTimeout(0),
@@ -51,6 +51,13 @@ void node_db_mysql::Connection::setWriteTimeout(const uint32_t writeTimeout) thr
5151
this->writeTimeout = writeTimeout;
5252
}
5353

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+
5461
void node_db_mysql::Connection::open() throw(node_db::Exception&) {
5562
this->close();
5663

@@ -84,7 +91,7 @@ void node_db_mysql::Connection::open() throw(node_db::Exception&) {
8491
mysql_options(this->connection, MYSQL_OPT_WRITE_TIMEOUT, (const char*) &this->writeTimeout);
8592
}
8693

87-
this->opened = mysql_real_connect(
94+
this->alive = mysql_real_connect(
8895
this->connection,
8996
this->hostname.c_str(),
9097
this->user.c_str(),
@@ -99,16 +106,16 @@ void node_db_mysql::Connection::open() throw(node_db::Exception&) {
99106
mysql_options(this->connection, MYSQL_OPT_RECONNECT, (const char*) &this->reconnect);
100107
#endif
101108

102-
if (!this->opened) {
109+
if (!this->alive) {
103110
throw node_db::Exception(mysql_error(this->connection));
104111
}
105112
}
106113

107114
void node_db_mysql::Connection::close() {
108-
if (this->opened) {
115+
if (this->alive) {
109116
mysql_close(this->connection);
110117
}
111-
this->opened = false;
118+
this->alive = false;
112119
}
113120

114121
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
125132
}
126133

127134
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);
132136
return version;
133137
}
134138

@@ -137,10 +141,6 @@ node_db::Result* node_db_mysql::Connection::query(const std::string& query) cons
137141
throw node_db::Exception("This binding needs to be linked with the thread safe MySQL library libmysqlclient_r");
138142
#endif
139143

140-
if (!this->opened) {
141-
throw node_db::Exception("Can't execute query without an opened connection");
142-
}
143-
144144
if (mysql_real_query(this->connection, query.c_str(), query.length()) != 0) {
145145
throw node_db::Exception(mysql_error(this->connection));
146146
}

src/connection.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class Connection : public node_db::Connection {
2121
void setSslVerifyServer(const bool sslVerifyServer) throw();
2222
void setTimeout(const uint32_t timeout) throw();
2323
void setWriteTimeout(const uint32_t writeTimeout) throw();
24+
bool isAlive(bool ping) throw();
2425
void open() throw(node_db::Exception&);
2526
void close();
2627
std::string escape(const std::string& string) const throw(node_db::Exception&);

wscript

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ from os.path import exists
1111

1212
srcdir = "."
1313
blddir = "build"
14-
VERSION = "0.6.1"
14+
VERSION = "0.6.2"
1515

1616
def set_options(opt):
1717
opt.tool_options("compiler_cxx")

0 commit comments

Comments
 (0)