From 08007ddc1703e6fc965eda1eca38ed25e360785e Mon Sep 17 00:00:00 2001 From: Jonas Lantto Date: Fri, 30 Dec 2016 16:49:06 +0100 Subject: [PATCH] disconnecting mongoose before attempting to reconnect on errors --- index.js | 12 +++++++----- package.json | 2 +- 2 files changed, 8 insertions(+), 6 deletions(-) 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": {