不要怂,就是干,撸起袖子干!

Commit 2bb1845c by Jan Aagaard Meier

Add a close method to sequelize to close the pool and unbind the process.exit handler. Closes #2020

1 parent 32594fea
......@@ -39,13 +39,22 @@ ConnectionManager = function(dialect, sequelize) {
if (config.pool.maxConnections) config.pool.max = config.pool.maxConnections;
if (config.pool.minConnections) config.pool.min = config.pool.minConnections;
this.onProcessExit = function() {
// Cleanup
self.pool.drain();
return;
}.bind(this);
process.on('exit', this.onProcessExit.bind(this));
};
ConnectionManager.prototype.onProcessExit = function() {
if (this.pool) {
this.pool.drain();
}
};
ConnectionManager.prototype.close = function () {
this.onProcessExit();
process.removeListener('exit', this.onProcessExit); // Remove the listener, so all references to this instance can be garbage collected.
process.on('exit', this.onProcessExit);
this.getConnection = function () {
return Promise.reject(new Error("ConnectionManager.getConnection was called after the connection manager was closed!"));
};
};
// This cannot happen in the constructor because the user can specify a min. number of connections to have in the pool
......
......@@ -802,5 +802,15 @@ module.exports = (function() {
}
};
/**
* Close all connections used by this sequelize instance, and free all references so the instance can be garbage collected.
*
* Normally this is done on process exit, so you only need to call this method if you are creating multiple instances, and want
* to garbage collect some of them.
*/
Sequelize.prototype.close = function () {
this.connectionManager.close();
};
return Sequelize;
})();
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!