• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 277
  • Last Modified:

locking files

RPG PROGRAM A, opens 30 files, it calls a CL which is Calling another CL, which tries to clear those files. It can not clear the because RPG still has them in the stack
  • 3
2 Solutions
Gary PattersonVP Technology / Senior Consultant Commented:
Yes, that's right.  By design, you cannot clear a file while it is open.  Clearing a file requires an exclusive lock.  

Generally you must close the files in RPGA before calling a program that may try to clear them.

One note, and I hesitate to mention it - is that you CAN delete all the rows in a file when the file is open (as opposed to clearing them) - except for any individual rows that might be locked at the moment.

If you use embedded SQL to clear the tables:


SQL will pick the best method.  Clearing is best, if the required exclusive lock can be obtained.  Clearing actually frees up the disk space, where deleting doesn't.  

If an exclusive lock cannot be obtained, SQL will automatically revert to single-row deletion, and delete each row individually.  This can take much longer, uses more CPU and disk IO, and will not free the disk space, but all the active records will be removed - and if the file is set up to reuse deleted records, the some or all of the deleted space will get reused the next time the file is populated.
Gary PattersonVP Technology / Senior Consultant Commented:
An alternative to closing and reopening files in RPGA (that might take some significant re-engineering depending on the program structure), one solution to this problem is to rearrange your program a little.

Instead of RPGA -> CL1 -> CL2, you may want to do something like this:

CL0 calls RPGA
   When user selects a function that may require clearing files,
   terminate RPGA with *INLR = *ON and return to CL0,
   setting a return value telling CL0 to call CL1

CL0 calls CL1call CL2
   Files get cleared

CL2 returns to CL1 returns to CL0

CL0 can now call RPGA again, or do whatever needs to be done at this point.
snigerAuthor Commented:
Thanks Gary,  how about putting everything in one activation group (CALLER activation group)

would this help ?
Gary PattersonVP Technology / Senior Consultant Commented:
Reasonable thought, but no not in this case.  Try it - it is what? - about 5 lines of code?


Fmyfile if              e disk
c                     read  myfile
c                     call 'TESTCLEAR'
c                     eval *INLR = *ON

Open in new window


clrpfm myfile

Open in new window

CLRPFM requires a system-side exclusive lock.  That means no locks on this file by anything in this job - same activation group or different activation group - or any lock in any other job on the system, either.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now