UPDATE I canβt check the code, but I give you some ideas: You are committing in connection not in db
# Get Cursor @contextmanager def get_cursor(): con = db.getconn() try: yield con finally: db.putconn(con) with get_cursor() as cursor: con.cursor.execute("INSERT INTO table (fields) VALUES (values) RETURNING id") con.commit() id = cursor.fetchone()
or
# Get Cursor @contextmanager def get_cursor(): con = db.getconn() try: yield con.cursor() con.commit() finally: db.putconn(con) with get_cursor() as cursor: con.cursor.execute("INSERT INTO table (fields) VALUES (values) RETURNING id") id = cursor.fetchone()
A connection pool exists because creating a new connection to db can be expensive and cannot avoid commits or rollbacks. Thus, you can fix your data without any problems, fixing the data will not lead to the destruction of the connection.
source share