This post answers much of what I want to ask on a general level, but seems to suggest it's also possible to put connections in a global if done correctly. I would like to ask if my particular assumptions about using pyodbc in this manner are correct.
In my case the connection is read-only, and there are never more than one query open at a time in a request. Given these constraints, is it safe to keep a pyodbc connection persistent in a global? The way I do it currently is this.
All queries goes trough a function db_execute()
This looks for a global class member glb.cnxn
and opens it if None
.
It then does cur = glb.cnxn.cursor()
and cur.execute()
returning the result.
The cursor is only used to get column names and never reused.
Is this safe?