-1

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?

Alias_Knagg
  • 886
  • 1
  • 7
  • 21

1 Answers1

0

For the record:

The danger with this approach is that a long running query will block glb.cnxn from use by other requests. So a second user that requests a page will have to wait for the first users query to finish.

The answer in the linked post using the g "request global" works much better.

Alias_Knagg
  • 886
  • 1
  • 7
  • 21