8000 Fix "conn pool exhausted" issue. (#2040) · angelafunk/python-docs-samples@695595a · GitHub
[go: up one dir, main page]

Skip to content

Commit 695595a

Browse files
jhm180Ace Nassri
authored andcommitted
Fix "conn pool exhausted" issue. (GoogleCloudPlatform#2040)
Updated code to return connection to pool before returning a value from the "With" block. When I tried running demo code in a gcloud function without using .putconn() to return the connection to the pool, 3 out of every 4 function invocations failed and returned error "connection pool exhausted" while fourth invocation worked successfully. Note that creating a cursor object using a With statement doesn't automatically return a connection to the pool after the With statement executes - psycopg/psycopg2#367
1 parent c2d8a9e commit 695595a

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

functions/sql/postgres_sample.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,10 @@ def postgres_demo(request):
6161

6262
# Remember to close SQL resources declared while running this function.
6363
# Keep any declared in global scope (e.g. pg_pool) for later reuse.
64-
with pg_pool.getconn().cursor() as cursor:
64+
with pg_pool.getconn() as conn:
65+
cursor = conn.cursor()
6566
cursor.execute('SELECT NOW() as now')
6667
results = cursor.fetchone()
68+
pg_pool.putconn(conn)
6769
return str(results[0])
6870
# [END functions_sql_postgres]

0 commit comments

Comments
 (0)
0