diff --git a/lib/mysql.js b/lib/mysql.js index 33117a2..b4420b0 100644 --- a/lib/mysql.js +++ b/lib/mysql.js @@ -530,7 +530,10 @@ MySQL.prototype.disconnect = function(cb) { debug('disconnect'); } if (this.client) { - this.client.end(cb); + this.client.end((err) => { + this.client = null; + cb(err); + }); } else { process.nextTick(cb); } diff --git a/test/connection.test.js b/test/connection.test.js index aac2493..92fd4cb 100644 --- a/test/connection.test.js +++ b/test/connection.test.js @@ -59,6 +59,41 @@ describe('connections', function() { }); }); + it('should disconnect then connect and ORM should work', function() { + var ds = new DataSource(mysqlConnector, config); + var Student = ds.define('Student', { + name: {type: String, length: 255}, + age: {type: Number}, + }, { + forceId: false, + }); + + return ds.connect() + .then(function(err) { + should.not.exist(err); + return ds.automigrate(['Student']); + }) + .then(function(err) { + should.not.exist(err); + should(ds.connected).be.True(); + return ds.disconnect(); + }) + .then(function(err) { + should.not.exist(err); + should(ds.connected).be.False(); + return ds.connect(); + }) + .then(function(err) { + should.not.exist(err); + should(ds.connected).be.True(); + return Student.count(); + }) + .then(function(count) { + should(count).be.a.Number(); + return ds.disconnect(); + }); + }); + it('should use latin1 charset', function(done) { var test_set = /latin1/; var test_collo = /latin1_general_ci/;