From bc220f5a640136945f46099ad4d0238d214eefc1 Mon Sep 17 00:00:00 2001 From: Antonio Trapani Date: Thu, 9 May 2019 13:26:56 +0200 Subject: [PATCH] fix: allow DataSource to reconnect properly Fixes issue where calling disconnect() method and then the connect() method causes the "Pool is closed" error. See issue #367. --- lib/mysql.js | 5 ++++- test/connection.test.js | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) 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/;