How does psycopg2.pool.SimpleConnectionPool manage the connections in the pool

48 Views Asked by At

As a connection pool, SimpleConnectionPool should take care of the connection management in the pool. For example,

  1. keep alive the connections in the pool
  2. evict the closed connections
  3. close the long idle connections and create new connections upon needed

But, when I look at the source code of SimpleConnectionPool, I didn't find the related code for the above connection management.

Not sure I have missed something, I can't believe that a connection pool doesn't take care of the connection manangement.

I write following code to test the connection management, an error occurred: c = conn.cursor() psycopg2.InterfaceError: connection already closed which means that the pool still uses the already closed connection.

So, I would ask: SimpleConnectionPool doesn't take care of the connection management? If so, then I can't use it in my long running web application.

I am really confused about whether SimpleConnectionPool can be used in the long running web application, some articles/blogs says that it can be use d in the web application such as

psycopg2 is a popular PostgreSQL adapter for Python that provides support for connection pooling through its psycopg2.pool module. Let's see how we can utilize psycopg2 to implement connection pooling in a web application. This is a simple FastAPI-based web application that manages blog posts.

from https://medium.com/datauniverse/optimizing-database-interaction-in-web-applications-connection-pooling-with-psycopg2-and-c56b37d155f8

Waiting for psycopg2 expert's explanation for this question, thanks! Or which connection pool can be used in the web application

import psycopg2.pool
import time

conf = {
    'dbname': "postgres",
    'user': 'postgres',
    'password': '1234',
    'host': 'localhost',
    'port': 5432,
    'sslmode': 'disable'
}

pool = psycopg2.pool.SimpleConnectionPool(
    1, 2, user='postgres', password='root',
    host='localhost', port='5432', database='postgres')

for i in range(5):
    conn = pool.getconn()
    # dsn = conn.dsn
    c = conn.cursor()
    sql = """
            
            select a,c from t_010 where c = '{}'  
            
            """.format(i * 10)
    c.execute(sql)
    rows = c.fetchall()
    c.close()
    pool.putconn(conn)
    # close conn so that conn in the pool can NOT be reused and should be evicted
    conn.close()
    for row in rows:
        print(row[0])
    print("start to sleep for {}".format(i))
    time.sleep(2)
    print("end to sleep for {}".format(i))

0

There are 0 best solutions below