brianc / node-postgres-docs

Documentation for node-postgres
https://node-postgres.com
MIT License
97 stars 94 forks source link

aws lambdas try to reconnect twice resulting in error #109

Closed WeaponizedLego closed 5 months ago

WeaponizedLego commented 4 years ago

I have a issue with trying to use this on Amazon Lambdas. When the Lambda runs first it connects and initiates the promise, but once it runs again to recieve the promise it tries to connect again resulting in this sweet little error message.

I've tried to use this solution to no it skips the first retry but eventually skips the if once enough ms has passed. Is it possible to use this package with lambdas? async query(query, params) { this.connection() // console.log(this.client) if (!this.client.connectionParameters.user.length > 0) { this.connection() } else { console.log('client is all ready connected, skipping connection') } const result = await this.client.query(query, params) this.endConnection() return result.rows }

{ "errorType": "Runtime.UnhandledPromiseRejection", "errorMessage": "Error: Client has already been connected. You cannot reuse a client.", "reason": { "errorType": "Error", "errorMessage": "Client has already been connected. You cannot reuse a client.", "stack": [ "Error: Client has already been connected. You cannot reuse a client.", " at Client._connect (/opt/nodejs/node_modules/pg/lib/client.js:85:19)", " at /opt/nodejs/node_modules/pg/lib/client.js:157:12", " at new Promise ()", " at Client.connect (/opt/nodejs/node_modules/pg/lib/client.js:156:12)", " at Battery.connection (/var/task/src/resources/postgress-db.js:26:24)", " at Battery.query (/var/task/src/resources/postgress-db.js:12:10)", " at Battery.fetchBatteryStatus (/var/task/src/resources/battery.js:24:30)", " at prepareResponse (/var/task/src/handlers/users/battery/get.handler.js:51:26)", " at processTicksAndRejections (internal/process/task_queues.js:97:5)" ] }, "promise": {}, "stack": [ "Runtime.UnhandledPromiseRejection: Error: Client has already been connected. You cannot reuse a client.", " at process. (/var/runtime/index.js:35:15)", " at process.emit (events.js:315:20)", " at process.EventEmitter.emit (domain.js:483:12)", " at processPromiseRejections (internal/process/promises.js:209:33)", " at processTicksAndRejections (internal/process/task_queues.js:98:32)" ] }

WeaponizedLego commented 4 years ago

I've created two pastebins with the code that hand the connection and executes queries. https://pastebin.com/QQXemsmB

And where I create the pg object https://pastebin.com/wSwGif09