Reputation: 157
I'm using cx_Oracle module in python. Do we need to close opened cursors explicitly? What will happen when we miss to close the cursor after fetching data and closing only the connection object (con.close()) without issuing cursor.close()?
Will there be any chance of memory leak in this situation?
Upvotes: 4
Views: 7024
Reputation: 773
According to the documentation of cx_Oracle, the cursor should be garbage-collected automatically, and there should be no risk of a leak.
However, in my anecdotal experience, if I didn't close the cursor explicitly, the memory usage of the process would grow without bounds -- this might have something to do with my usage of a custom rowfactory
, where I had to capture the cursor in a lambda (although, in theory, the GC should be able to handle this case as well).
Since the Cursor class implements the context manager pattern, you can safely and conveniently write:
with connection.cursor() as cursor:
cursor.execute("...")
Upvotes: 2
Reputation: 5890
If you use multiple cursor. cursor.close() will help you to release the resources you don't need anymore. If you just use one cursor with one connection. I think connection.close() is fine.
Upvotes: 1