Skip to content

Commit 08007dd

Browse files
committed
disconnecting mongoose before attempting to reconnect on errors
1 parent 8f8083c commit 08007dd

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

index.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,13 @@ function _connect (options) {
5959
mongoose.connection.on('error', dbErr => {
6060
log.warn({ err: dbErr.message }, 'DB connection error, retrying in 30 seconds')
6161
isOk = false
62-
// reject(dbErr)
63-
setTimeout(function () {
64-
mongoose.connect(dbUri, dbOptions)
65-
log.info('Attempting to reconnect')
66-
}, options.reconnectInterval || RECONNECT_TIMEOUT)
62+
// disconnect if connection is still open
63+
mongoose.disconnect().then(() => {
64+
setTimeout(function () {
65+
mongoose.connect(dbUri, dbOptions)
66+
log.info('Attempting to reconnect')
67+
}, options.reconnectInterval || RECONNECT_TIMEOUT)
68+
})
6769
})
6870

6971
mongoose.connection.on('connected', () => {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "kth-node-mongo",
3-
"version": "1.0.3",
3+
"version": "1.0.4",
44
"description": "Database connection module for Node.js applications using Mongoose.",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)