0
import psycopg2
from psycopg2 import pool
connection_pool = psycopg2.pool.ThreadedConnectionPool(1, 50, user='A', password='S', host='D',port='D', database='AD')

def fetch_data(id):
    try:
        sql = build_sql_query('SELECT * FROM USERS')
        connection = connection_pool.getconn()
        cursor = connection.cursor()
        cursor.execute(sql)
        records = cursor.fetchall()
    except (Exception, psycopg2.Error) as error :
        print ("Error while connecting to PostgreSQL", error)

我不想断开连接。想用生命周期与 postgres 建立全球联系。我已经尝试了很多,但如果有的话,请告诉我。

4

1 回答 1

0

在我手中,它不会每次都断开连接。他们只是积累。

你所要做的:

connection_pool.putconn(connection)

当您完成连接时。也许你认为当它超出范围时应该自动发生,但显然它没有。

于 2021-03-10T17:27:28.910 回答