简体   繁体   中英

cx_Oracle module cursor close in python

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?

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.

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("...")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM