I'm rewriting a program in Python; the original program is written in C with embedded PL/SQL statements making calls to Oracle database.
In the original program, host variables are declared which are passed to Oracle and back, with their values being updated by Oracle stored procedures/functions. The program disconnects and reconnects to the database regularly, and when it does so all of these host variables maintain their values.
When recreating this in Python, I have to create in/out bind variables using python-oracledb's cursor.var() method. Whilst I've got these working fine, I don't understand what is actually happening under the hood, as a result I'm unsure how to approach cursor closing & connection handling in my Python script.
It appears that a cursor object is tied to a connection object.
So when I close and reopen the connection, I would assume that the variables tied to the cursor (created using cursor.var) would be lost. However I've tried this out and they appear to maintain their value. Why is this?
When I asked ChatGPT about this it said it shouldn't happen, but as ChatGPT is out of date (using cx_oracle instead) it appears this functionality is a new feature; I can't find anything on google to confirm this however. So my questions is, what exactly is happening when I call connection.cursor() and cursor.var(); what are these objects being created, and how are they affected by disconnecting and reconnecting to the database?
I'm a rookie software dev and haven't studied much computer science, so if these questions are due to a lack of background understanding then please could you point me in the direction of some resources that will help me better understand whats happening!
The test code I made to check if values were maintained is as follows:
import oracledb # allows python to talk to oracle
import connection_config # username and database id for connection
con = oracledb.connect(user=connection_config.user, password=connection_config.pw, dsn=connection_config.dsn)
cursor = con.cursor() # connection cursor
host_variable = cursor.var(str)
host_variable.setvalue(0,'VALUE')
print(host_variable.getvalue())
con.close()
con = oracledb.connect(user=connection_config.user, password=connection_config.pw, dsn=connection_config.dsn)
cursor = con.cursor() # connection cursor
print(host_variable.getvalue())
Both print statements print 'VALUE'. According to chatgpt this wouldn't happen with the old oracle driver, cx_oracle.
TIA!