when to disconnect and when to end a pg client or pool

前端 未结 4 537
囚心锁ツ
囚心锁ツ 2021-02-01 02:38

My stack is node, express and the pg module. I really try to understand by the documentation and some outdated tutorials. I dont know when and how to disconnect and to e

4条回答
  •  天命终不由人
    2021-02-01 03:17

    Its quite simple, a client-connection (single connection) opens up, query with it, once you are done you end it.

    The pool concept is different, in the case of mysql : you have to .release() the connection back to the pool once you are done with it, but it seems that with pg is a different story:

    From an issue on the github repo : Cannot use a pool after calling end on the pool #1635

    "Cannot use a pool after calling end on the pool"

    You can't reuse a pool after it has been closed (i.e. after calling the .end() function). You would need to recreate the pool and discard the old one.

    The simplest way to deal with pooling in a Lambda is to not do it at all. Have your database interactions create their own connections and close them when they're done. You can't maintain a pool across freeze/thaw cycles anyway as the underlying TCP sockets would be closed.

    If opening/closing the connections becomes a performance issue then look into setting up an external pool like pgbouncer.

    So I would say that your best option is to not end the pool, unless you are shutting down the server

提交回复
热议问题