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.
This commit is contained in:
Antonio Trapani 2019-05-09 13:26:56 +02:00 committed by Biniam Admikew
parent 4c4094a310
commit bc220f5a64
2 changed files with 39 additions and 1 deletions

View File

@ -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);
}

View File

@ -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/;