diff --git a/index.js b/index.js index 81d5f3b..e26ac16 100644 --- a/index.js +++ b/index.js @@ -59,11 +59,13 @@ function _connect (options) { mongoose.connection.on('error', dbErr => { log.warn({ err: dbErr.message }, 'DB connection error, retrying in 30 seconds') isOk = false - // reject(dbErr) - setTimeout(function () { - mongoose.connect(dbUri, dbOptions) - log.info('Attempting to reconnect') - }, options.reconnectInterval || RECONNECT_TIMEOUT) + // disconnect if connection is still open + mongoose.disconnect().then(() => { + setTimeout(function () { + mongoose.connect(dbUri, dbOptions) + log.info('Attempting to reconnect') + }, options.reconnectInterval || RECONNECT_TIMEOUT) + }) }) mongoose.connection.on('connected', () => { diff --git a/package.json b/package.json index fa1809b..b8d960b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "kth-node-mongo", - "version": "1.0.3", + "version": "1.0.4", "description": "Database connection module for Node.js applications using Mongoose.", "main": "index.js", "scripts": {