vertica / vertica-nodejs

Official native node.js client for the Vertica Analytics Database.
https://www.vertica.com/
Apache License 2.0
12 stars 14 forks source link

Keep connection pool alive #108

Open negreanucalin opened 1 year ago

negreanucalin commented 1 year ago

I have made an express server and noticed that when the Pool is not being used for ~30 minutes an error spring up:

this.pool = new Pool({
    max: process.env.V_POOL_SIZE,
    idleTimeoutMillis: 0,
    connectionTimeoutMillis:0,
    allowExitOnIdle:false,
    host
});

Stack trace

/usr/src/app/node_modules/vertica-nodejs/lib/client.js:203
const error = this._ending ? new Error('Connection terminated') : new Error('Connection terminated unexpectedly')
                                                                        ^
Error: Connection terminated unexpectedly
    at Connection.<anonymous> (/usr/src/app/node_modules/vertica-nodejs/lib/client.js:203:73)
    at Object.onceWrapper (node:events:627:28)
    at Connection.emit (node:events:513:28)
    at Connection.emit (node:domain:489:12)
    at Socket.<anonymous> (/usr/src/app/node_modules/vertica-nodejs/lib/connection.js:181:12)
    at Socket.emit (node:events:525:35)
    at Socket.emit (node:domain:489:12)
    at endReadableNT (node:internal/streams/readable:1358:12)
    at processTicksAndRejections (node:internal/process/task_queues:83:21)

Is there a way to prevent this? (listen to an event and reconnect, disable auto-closing connections or catch and reconnect)

Dependencies:

    "@types/express": "^4.17.15",
    "@types/jest": "^29.2.1",
    "@types/node": "^18.11.9",
    "@types/pg-copy-streams": "^1.2.1",
    "date-fns": "^2.29.3",
    "dotenv": "^16.0.3",
    "express": "^4.18.2",
    "jest": "^29.2.2",
    "kafkajs": "^2.2.3",
    "logform": "^2.4.2",
    "moment": "^2.29.4",
    "mysql2": "^2.3.3",
    "nodemon": "^2.0.20",
    "pg-copy-streams": "^6.0.4",
    "ts-command-line-args": "^2.3.1",
    "ts-jest": "^29.0.3",
    "ts-node": "^10.9.1",
    "typescript": "^4.8.4",
    "vertica-nodejs": "^1.0.1",
    "winston": "^3.8.2"

Versions Node v16.19.0 Vertica 11

negreanucalin commented 1 year ago

Found a bit of a dirty fix:

process.on('uncaughtException', async (err ) => {
    if (err.message === 'Connection terminated unexpectedly') {
        console.log(`Restart vertica connection`)
        await verticaCon.disconnect(); // I'm calling the done() method from pool.connect()
        await verticaCon.connect();    // I'm calling this.pool.connect(err: any, client: any, done: Function) => {})
    }
})