Skip to content

Instantly share code, notes, and snippets.

@phpdave
Last active May 10, 2018 22:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phpdave/f38ecf9a56ab4193462d to your computer and use it in GitHub Desktop.
Save phpdave/f38ecf9a56ab4193462d to your computer and use it in GitHub Desktop.
Pseudo logic - Kill all jobs on a file in IBMi 7.2 to remove record locks
CREATE PROCEDURE MYLIB.KILLJOBSGIVENFILE ( )
LANGUAGE SQL
SPECIFIC MYLIB.KILLJOBSSP
BEGIN
--Variable to control loop
DECLARE END_TABLE INT DEFAULT 0;
--If there's no record update END_TABLE to 1 to end the loop
DECLARE CONTINUE HANDLER FOR NOT FOUND
SET END_TABLE = 1;
--Get Job Names based on library and file
DECLARE C1 CURSOR FOR
SELECT JOB_NAME
FROM QSYS2.RECORD_LOCK_INFO
WHERE SYS_DNAME = 'MYLIB'
AND SYS_TNAME = 'MYFILE'
OPEN C1 ;
--optional lock the user that is trying to access
CALL QSYS2.QCMDEXC('ALCOBJ OBJ((QSYS/WEBUSER *USRPRF *EXCL))');
FETCH C1 INTO JOB_NAME;
WHILE END_TABLE = 0 DO
--Kill jobs
CALL QSYS2.QCMDEXC('ENDJOB JOB(' || JOB_NAME || ') OPTION(*IMMED)');
FETCH C1 INTO JOB_NAME;
END WHILE;
--Wait 10 seconds for jobs to clear
CALL QSYS2.QCMDEXC('DLYJOB DLY(10)');
--Do what you want with the file
--HERE!
--Let the user back in
DLCOBJ OBJ((QSYS/WEBUSER *USRPRF *EXCL))
CLOSE C1;
RETURN ;
END ;
--Run Stored Proc
CALL MYLIB.KILLJOBSGIVENFILE ();
--https://www-01.ibm.com/support/knowledgecenter/ssw_i5_54/cl/endjob.htm
--https://www.ibm.com/developerworks/community/wikis/home?lang=en#!/wiki/IBM%20i%20Technology%20Updates/page/DB2%20for%20i%20Services%20-%20System%20names%20for%20new%20files%20in%20QSYS2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment