Skip to content
Advertisement

cx_Oracle.DatabaseError: DPI-1039: statement was already closed

Upgraded to latest Cx_Oracle release (5.1.3 to 7.1.3) and now all my code is broken. Problem appears to be occurring when attempting to pass a cursor.

JavaScript

I’m getting correct data in the fetchall so I know it is connecting properly and returning a valid result. So I comment it out.

In the other scripts I call it and rows should now store the cursor correct?

JavaScript

When I fetchall() from the other script I get below and get the error.

JavaScript

cx_Oracle.DatabaseError: DPI-1039: statement was already closed

Any ideas?

full db.py

JavaScript

In another python I create a new DB and try to get the cursor back.

JavaScript

Result:

JavaScript

If I return fetchall() the data comes across. When I pass the cursor itself the error occurs.

JavaScript

Any ideas why can’t I pass the cursor as it was done in the past?

Advertisement

Answer

Your error has to do with the with-block (also known as a context manager). Let me add a couple comments to the select_query_remy function:

JavaScript

Since the with-block manages the context of the database connection, the connection will be closed after the block is exited.

To fix the error, I suggest performing fetchall inside the function directly then working with that data outside the function. (You have already kinda figured this out.)

Further Reading: The Python Language Reference: The with statement

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement