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.