I'm creating a project using the node-postgress documentation:
https://node-postgres.com/features/connecting
in the documentation shows an example of a query using pool where at the end of the request it terminates the connection with the pool.end ();
I followed the same example and it worked, but when I try to do a second query this error occurs:
Cannot use a pool after calling end on the pool
Error: Cannot use a pool after calling end on the pool
at Pool.connect (c:\Users\Danilo Torquato\Documents\projetos\elton\MaisCidadao\node_modules\pg-pool\index.js:141:19)
at Pool.query (c:\Users\Danilo Torquato\Documents\projetos\elton\MaisCidadao\node_modules\pg-pool\index.js:251:10)
at StudentController.getAll (c:\Users\Danilo Torquato\Documents\projetos\elton\MaisCidadao\controllers\StudentController.js:11:16)
at Layer.handle [as handle_request] (c:\Users\Danilo Torquato\Documents\projetos\elton\MaisCidadao\node_modules\express\lib\router\layer.js:95:5)
at next (c:\Users\Danilo Torquato\Documents\projetos\elton\MaisCidadao\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (c:\Users\Danilo Torquato\Documents\projetos\elton\MaisCidadao\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (c:\Users\Danilo Torquato\Documents\projetos\elton\MaisCidadao\node_modules\express\lib\router\layer.js:95:5)
at c:\Users\Danilo Torquato\Documents\projetos\elton\MaisCidadao\node_modules\express\lib\router\index.js:281:22
at Function.process_params (c:\Users\Danilo Torquato\Documents\projetos\elton\MaisCidadao\node_modules\express\lib\router\index.js:335:12)
at next (c:\Users\Danilo Torquato\Documents\projetos\elton\MaisCidadao\node_modules\express\lib\router\index.js:275:10)
In a long running program you don’t need to call ‘pool.end()’ unless you’re going to dynamically create and gracefully shutdown a pool. In most cases you can ignore it entirely as exiting the node process will close the sockets for the pool.
in the documentation shows an example of a query using pool where at the end of the request it terminates the connection with the pool.end ();
The documentation shows an example script you can run standalone; there’s no request involved. pool.end() should be called when you don’t intend to use the pool anymore, so in a web server, you would call it when shutting down (or not at all).
Most helpful comment
In a long running program you don’t need to call ‘pool.end()’ unless you’re going to dynamically create and gracefully shutdown a pool. In most cases you can ignore it entirely as exiting the node process will close the sockets for the pool.