Is it possible to perform additional operations on job cancellation?
The context: From time to time a read operation from the database hangs up for eternity with no apparent reason. The read operation is performed with sqlread function, and (according to the docs) there is no way to set a timeout on this operation. So my idea was the following: read from the database in a separate thread and set a timeout on this thread. This is accomplished as per code snippet below:
pool = gcp();
f = parfeval(pool, @dbRead, 1, datasource,username,password);
ok = wait(f, 'finished', 60);
result = fetchOutput(f);
function [result] = dbRead(datasource,username,password)
conn = database(datasource,username,password);
result = sqlread(conn, 'tablename');
The problem with this approach is that I'm opening the database connection inside the dbRead function and, if the job is cancelled, the connection does not get closed.
Approach 1 - Pass connection as a parameter to dbRead function - Not possible in my version of Matlab (results in an error). A function that would allow me to do so www.mathworks.com/help/database/ug/createconnectionforpool.html was introduced in R2019a; unfortunately upgrading my Matlab (2018b) to a higher version is not an option.
Approach 2 - Keep connection as a global variable - Results in an error, precisely:
Error using database.odbc.connection/sqlread (line 62)
To sum up, my question is - is it possible to run additional code / function on job cancellation?
What other approach could I take to accomplish my goal of terminating the database read if it hangs?