Closing VSAM files

Use the CLOSE statement to disconnect your program from a VSAM file. If you try to close a file that is already closed, you will get a logic error. Check the file status key after each CLOSE statement.

About this task

If you do not close a VSAM file, the file is automatically closed for you under the following conditions:

  • When the run unit ends normally, all open files defined in any COBOL programs in the run unit are closed.
  • When the run unit ends abnormally, if the TRAP(ON) runtime option has been set, all open files defined in any COBOL programs in the run unit are closed.
  • When Language Environment® condition handling has completed and the application resumes in a routine other than where the condition occurred, open files defined in any COBOL programs in the run unit that might be called again and reentered are closed.

    You can change the location where a program resumes after a condition is handled. To make this change, you can, for example, move the resume cursor with the CEEMRCR callable service or use language constructs such as a C longjmp statement.

  • When you issue CANCEL for a COBOL subprogram, any open nonexternal files defined in that program are closed.
  • When a COBOL subprogram with the INITIAL attribute returns control, any open nonexternal files defined in that program are closed.
  • When a thread of a multithreaded application ends, both external and nonexternal files that were opened from within that same thread are closed.

File status key data items in the DATA DIVISION are set when these implicit CLOSE operations are performed, but your EXCEPTION/ERROR declarative is not invoked.

Errors: If you open a VSAM file in a multithreaded application, you must close it from the same thread of execution. Attempting to close the file from a different thread results in a close failure with file-status condition 90.