Working with temporary files may confuse some AS/400 programmers. Suppose you run the Display Object Description (DSPOBJD) command, directing output to an outfile, because you need to process a series of objects. Where do you put the outfile (a temporary file), and what do you call it? This article addresses these questions.
Any IBM-supplied command that produces an outfile uses a model file. Model files are empty physical files (they have no members) that IBM has placed in library QSYS. Model files have a rather elaborate record layout, full of useful fields that you can put to immediate use. For instance, the DSPOBJD command uses a model file named QADSPOBJ, which has, among its many fields, the date the object was created, its size, the name of its owner, the date of last use, and the date it was last backed up. (It's easy to determine the name of the model file: Prompt for the command, move the cursor to the OUTFILE parameter, and press the Help key.)
You don't have to do anything special to use all these fields. Whenever you run DSPOBJD with output to an outfile, OS/400 automatically creates an empty file using QADSPOBJ as a model and then populates the file with valid information. The problem is, rather, how to use the file. The problem is that temporary files are created, more often than not, in library QTEMP. Due to QTEMP's nature, files in QTEMP are ephemeral. Consider the following scenario:
Imagine you are writing code for a new application that uses a list of objects created with DSPOBJD. In a CL program, you code a DSPOBJD command to write an outfile named OBJECTSinQTEMP,andthenyoucallanRPGprogramtoprocessthatfile.IntheRPG program, you code OBJECTS in the F-spec and use external file definition. Yet the RPG program fails to compile, because there's no OBJECTS file anywhere-why should there be? You haven't yet run DSPOBJD to create OBJECTS.
Aha! You decide, then, to use the Override with Database File (OVRDBF) command from the command line before compiling the RPG program, like this:
OVRDBF +
FILE(OBJECTS) +
TOFILE(QADSPOBJ)
Then, you submit the compile to batch. The compilation fails again, because the file override applies to the interactive job you're running at your display station, not to the batch job you're submitting therefrom. You have to compile the RPG program interactively, and that's a sure way to get yourself in trouble with other programmers, your boss, and (worst of all) the end users.
To avoid these problems, it's best that you give the outfile the same name as the model file- QADSPOBJ. That done, you can compile the RPG program without trouble, because the compiler finds QADSPOBJ in QSYS (it doesn't matter to the compiler or the program whether the file it finds is empty or not).
One question I haven't addressed yet is where to put the outfile. Do yourself a favor and don't place it in (a) any IBM-supplied library, such as QGPL or QUSRSYS, or (b) any user-created libraries. The reason for not placing the temporary file in user libraries is that such libraries are permanent in nature. If you create an outfile in, say, QGPL, the outfile remains there after completion of your job. Worse, if someone else wants to run the same application at the same time, the second job's outfile wipes out the data from the first one, perhaps before the first job has a chance to use the data. Or you may run into object lock contention, and so on. It's a headache you can avoid by placing your outfiles in QTEMP. QTEMP is especially useful for this type of application:
o It creates a private file that only one job can process. Two or more users may be running the same application, all creating the same file in QTEMP, and no conflict is created. That's because OS/400 maintains a separate QTEMP library for each job, whether it's interactive or batch. Using QTEMP, therefore, eliminates conflicts between jobs.
o If the job aborts for whatever reason, the temporary file is immediately deleted; the system doesn't keep leftover junk files that might accumulate and waste disk space.
Logs and accounting journals (not AS/400 journals!) require a different kind of temporary file. By definition, a log or a journal contains data that is relevant only for a certain period of time. Let's see how we can address this issue.
The problem arises when you need to accumulate data across job boundaries. For instance, suppose you need to keep a daily log of cash transactions and you want to keep a week's worth of them. QTEMP is out of the picture in a case such as this, because several users might want to record cash transactions, but QTEMP is private to each job and is destroyed when the user signs off. Clearly, you need to store the cash transactions in a "regular" user library. Let's say you have a library named CASHMGT and you decide to put the cash transaction log there.
You could design the cash log so it includes a date field to store the date each transaction was logged. The problem here is one of maintenance-on the eighth day, you'll have to rid the log of the cash transactions logged on the first day. This means writing a program to delete those records (or using SQL or a Query Management query) and reorganizing the file to reclaim the space the deleted records occupied.
It's easier to use separate members instead. Each day, you could add a member to the cash log and store the transactions in that member. When you need to clean up old records, all you'll have to do is remove the member using a single command-Remove Member (RMVM). For
simplicity's sake, give each member a name that can be derived automatically. For example, they could be named CASHmmdd, where mmdd represents the month and day of the system date. This way, you can retrieve system values QMONTH and QDAY prior to writing a cash transaction, build the name of the member, and write records to this unique member.
There are three problems with using separate members, however:
o You have to use the OVRDBF command to point to the correct member within the file. If you neglect to do this, transactions will be logged to the first member the system finds, which is probably wrong.
o If you need to write a program that reads cash transactions for more than one day, your program needs to read records from more than one member. But a high-level language's (HLL's) view of a file reaches the end-of-file condition (*INLR in RPG) when the current member ends. To solve the problem, use OVRDBF before calling the HLL program, making sure to specify MBR(*ALL). Your HLL program won't reach end-of-file until all members have been processed.
o If you need a logical view of the cash transactions, the logical file member may have to span more than one physical file member-and the limit is 32 members.
You have to weigh the pros and cons of each approach to see which one is best for you and your installation. For example, if you run a 24/7 installation (i.e., one that works 24 hours a day, seven days a week), you cannot employ a method that requires using the Reorganize Physical File Member (RGZPFM) command, since this command attempts to place an exclusive lock on the member. Such a lock may be impossible to obtain if someone is writing records to the cash log, which would make RGZPFM fail. Or RGZPFM may obtain an exclusive lock on the member, only to prevent a user from logging cash transactions at the time they happened-which could be crucial if the time is two minutes before midnight.
Ernie Malaga is a senior technical editor for Midrange Computing. He may be reached by fax at 305-387-8263 or by email at
LATEST COMMENTS
MC Press Online