Skip to content

Commit

Permalink
allow reconnecting after failed connect.
Browse files Browse the repository at this point in the history
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
ssinghi authored and Sannis committed Aug 14, 2010
1 parent fe34071 commit 6c7643f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/mysql_bindings_connection.cc
Expand Up @@ -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;
}

Expand Down
5 changes: 1 addition & 4 deletions tests/test-class-mysqlconn.js
Expand Up @@ -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();
};
Expand Down
10 changes: 10 additions & 0 deletions tests/test-connect-different-args.js
Expand Up @@ -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.