use finally: close

This commit is contained in:
GroovieGermanikus 2023-11-30 17:09:16 +01:00
parent a572cbeb69
commit 4afdf9fa5a
No known key found for this signature in database
GPG Key ID: 5B6EB831A5CD2015
1 changed files with 8 additions and 10 deletions

View File

@ -29,10 +29,10 @@ def _init_pool():
database = environ.get('PGDATABASE', 'mangolana')
ssl_context = _configure_sslcontext()
application_name = "bankingstage-dashboard"
pool = PooledDB(pg8000, pool_size,
the_pool = PooledDB(pg8000, pool_size,
database=database, user=username, password=password, host=host, port=port, application_name=application_name, ssl_context=ssl_context)
print("Initialized database connection pool with size ", pool_size)
return pool
return the_pool
pool = _init_pool()
@ -47,17 +47,15 @@ def query(statement, args=[]):
try:
cursor.execute(statement, args=args)
elapsed_total = time.time() - start
keys = [k[0] for k in cursor.description]
maprows = [dict(zip(keys, copy.deepcopy(row))) for row in cursor]
except Exception as ex:
print("Exception executing query:", ex)
return []
elapsed_total = time.time() - start
keys = [k[0] for k in cursor.description]
maprows = [dict(zip(keys, copy.deepcopy(row))) for row in cursor]
cursor.close()
con.close()
finally:
cursor.close()
con.close()
if elapsed_total > .2:
print("Database Query took", elapsed_total, "secs", "(", elapsed_connect, ")")