필터 지우기
필터 지우기

Is it possible to perform additional operation on job cancellation?

조회 수: 1 (최근 30일)
Agata Migalska
Agata Migalska 2019년 11월 26일
편집: Philippe Lebel 2019년 12월 3일
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(); % create a thread pool
f = parfeval(pool, @dbRead, 1, datasource,username,password); % start an asynchronous read job
ok = wait(f, 'finished', 60); % set the timeout of 60 sec
if ~ok % if timeout out, cancel the job
cancel(f);
else % otherwise fetch the results
result = fetchOutput(f);
end
delete(f); % delete the job
function [result] = dbRead(datasource,username,password)
conn = database(datasource,username,password); % connect to the database
result = sqlread(conn, 'tablename'); % read the table
close(conn); % close the connection
end
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:
Caused by:
Error using database.odbc.connection/sqlread (line 62)
Invalid connection.
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?

답변 (1개)

Philippe Lebel
Philippe Lebel 2019년 11월 26일
  댓글 수: 2
Agata Migalska
Agata Migalska 2019년 12월 2일
편집: Agata Migalska 2019년 12월 3일
I accepted the answer earlier, but having tested more thoroughly I'm afraid it is not the right answer.
oncleanup function works nicely when there is an exception thrown inside a function (here: inside dbRead). Unfortunately oncleanup function is not called on job cancel.
Philippe Lebel
Philippe Lebel 2019년 12월 3일
편집: Philippe Lebel 2019년 12월 3일
Have you tried setting callbacks on your job?
I don't know if a cancelled job results in the "jobfinished" callback function being called though.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by