Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

278
Vistas
How to make a connection pool in psycopg2 that can handle a database restart

I'm using psycopg2 in a FastAPI application, witha ThreadedConnectionPool.
I use putconn(conn) in normal use, but when the DB restarts, the connection stays open and the pool fills up. Then the FastAPI application can't get a new connection from the pool, with a "Connection pool exhausted" error. Restarting the FastAPI app clears the pool, but how can I do it automatically?

What I've done so far:
I have one function, which runs on FastAPI startup:

@app.on_event("startup")
def open_connection_pool():
    try:
        global LOCAL_CON_POOL
        LOCAL_CON_POOL = psycopg2.pool.ThreadedConnectionPool(1,3,CONNSTRING)
    except Exception as ex:
        logging.error(ex)

Then I use the LOCAL_CON_POOL:

def get_db_conn_from_pool():
    try:
        local_conn = LOCAL_CON_POOL.getconn()
        return local_conn
    except Exception as ex:
        try:
            putconn(local_conn)
        except Exception as ex1:
            logging.error(ex1)
            rasie ex1

Then I use the connection:

def get_db_data():
    try:
        node_conn = get_db_conn_from_pool()
        with node_conn.cursor() as node_cur:
            node_cur.execute(query)
            data = node_cur.fetchone()[0]
        LOCAL_CON_POOL.putconn(node_conn)
        return data
    except exception as ex:
        logging.error("Couldnt get data from db")

Putting a putconn() in a finally block within get_db_data() results in a variable referenced before assignment.

over 4 years ago · Santiago Trujillo
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda