What happens if you don't close a pyodbc connection?

17.2k views Asked by At

Just wondering what happens if a connection is not properly closed in pyodbc.

Also, do i need to close the cursor before the connection?

In my particular use case I included a call to close the connection in a custom DB Class in the .__del__() method, but do not explicitly call close.

What's best practice?

1

There are 1 answers

9
FlipperPA On

Connections (and their associated cursors) are automatically closed when they are deleted, so it cleans up behind itself. However, if you're connecting in more than one place, you'll want to close explicitly. Also, to be more Pythonic, it is always better to be explicit.

Also note: closing a connection without committing your changes will result in an automatic implicit rollback.

For more, and reference:

https://github.com/mkleehammer/pyodbc/wiki/Connection#close