I have trouble with a database connection. I have a script that executes another script (below) 2000 times. I'm using pg database. I have set the limit number of connection to 5 maximum. But it look like there is more than 5 connections at same time because my cpu is running at 100% and in stats of my database that say I have more than 100 connections in same time.
So I'm wondering if the keyword "with" closes the connection if there is an error in my SQL?
Here my code that is executed 2000 times by another script:
try:
engine = create_database_connection()
with engine.connect() as connection:
groups_sql = connection.execute(
text('SELECT uuid, name FROM user_group WHERE project_id = :project_id'),
project_id=project['uuid']
).fetchall()
except:
print('error')