mwoods
mwoods

Reputation: 317

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

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?

Upvotes: 12

Views: 18060

Answers (1)

FlipperPA
FlipperPA

Reputation: 14311

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

Upvotes: 11

Related Questions