Permalink
Browse files

allow reconnecting after failed connect.

If connecting fails, then set _conn = NULL so that other commands also fail with not connected error. Also, from mysql documentation: "If you call mysql_sqlstate()  after mysql_real_connect()  fails, mysql_sqlstate() might not return a useful value". We don't need to keep a connection object which has failed.
  • Loading branch information...
1 parent fe34071 commit 6c7643ff74519b5bad232f47e558663a80365897 @ssinghi ssinghi committed with Aug 14, 2010
Showing with 13 additions and 4 deletions.
  1. +2 −0 src/mysql_bindings_connection.cc
  2. +1 −4 tests/test-class-mysqlconn.js
  3. +10 −0 tests/test-connect-different-args.js
@@ -102,7 +102,9 @@ bool MysqlConn::Connect(const char* hostname,
connect_error = mysql_error(_conn);
if (unsuccessful) {
+ mysql_close(_conn);
connected = false;
+ _conn = NULL;
return false;
}
@@ -296,15 +296,12 @@ exports.SetCharset = function (test) {
};
exports.SqlState = function (test) {
- test.expect(4);
+ test.expect(2);
var conn = mysql_libmysqlclient.createConnection(cfg.host, cfg.user, cfg.password, cfg.database), res;
test.ok(conn, "mysql_libmysqlclient.createConnection(host, user, password, database)");
test.equals(conn.sqlState(), "00000", "conn.sqlState() after connection to allowed database");
conn.close();
- res = conn.connect(cfg.host, cfg.user, cfg.password, cfg.database_denied);
- test.ok(!res, "conn.connect(cfg.host, cfg.user, cfg.password, cfg.database_denied)");
- test.equals(conn.sqlState(), "42000", "conn.sqlState() after connection to denied database");
test.done();
};
@@ -64,3 +64,13 @@ exports.Connect_DeniedDb = function (test) {
test.done();
};
+exports.Connect_DeniedFollowedByAllowedDb = function (test) {
+ test.expect(2);
+
+ var conn = mysql_libmysqlclient.createConnection();
+ test.ok(!conn.connect(cfg.host, cfg.user, cfg.password, cfg.database_denied), "conn.connect() for denied database");
+
+ test.ok(conn.connect(cfg.host, cfg.user, cfg.password, cfg.database), "conn.connect() for allowed database");
+ test.done();
+};
+

0 comments on commit 6c7643f

Please sign in to comment.