4

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?

2 Answers 2

2

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("...")
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.