Sharing an Open Data Path

An open data path is the path through which all input and output operations for a file are performed. Usually a separate open data path is defined each time a file is opened. If you specify SHARE(*YES) for the file creation or on an override, the first program’s open data path for the file is shared by subsequent programs that open the file concurrently.

If you are sharing your files so that you can use them in different programs or modules, consider passing the files between your programs and modules as parameters instead. See Passing File Parameters.

The position of the current record is kept in the open data path for all programs using the file. If you read a record in one program and then read a record in a called program, the record retrieved by the second read depends on whether the open data path is shared. If the open data path is shared, the position of the current record in the called program is determined by the current position in the calling program. If the open data path is not shared, each program has an independent position for the current record.

If your program holds a record lock in a shared file and then calls a second program that reads the shared file for update, you can release the first program's lock by :
  • performing a READ operation on the update file by the second program, or
  • using the UNLOCK or the read-no-lock operations.

In ILE, shared files are scoped to either the job level or the activation group level. Shared files that are scoped to the job level can be shared by any programs running in any activation group within the job. Shared files that are scoped to the activation group level can be shared only by the programs running in the same activation group.

The default scope for shared files is the activation group. For job-level scope, specify OVRSCOPE(*JOB) on the override command.

ILE RPG offers several enhancements in the area of shared ODPs. If a program or procedure performs a read operation, another program or procedure can update the record as long as SHARE(*YES) is specified for the file in question. In addition, when using multiple-device files, if one program acquires a device, any other program sharing the ODP can also use the acquired device. It is up to the programmer to ensure that all data required to perform the update is available to the called program.

If a program performs a sequential input operation, and it results in an end-of-file condition, the normal operation is for any subsequent sequential input operation in the same module to immediately result in an end-of-file condition without any physical input request to the database. However, if the file is shared, the RPG runtime will always send a physical input request to the database, and the input operation will be successful if the file has been repositioned by a call to another program or module using the shared file.

Sharing an open data path improves performance because the IBM® i system does not have to create a new open data path. However, sharing an open data path can cause problems. For example, an error is signaled in the following cases:
  • If a program sharing an open data path attempts file operations other than those specified by the first open (for example, attempting input operations although the first open specified only output operations)
  • If a program sharing an open data path for an externally described file tries to use a record format that the first program ignored
  • If a program sharing an open data path for a program described file specifies a record length that exceeds the length established by the first open.

When several files in one program are overridden to one shared file at run time, the file opening order is important. In order to control the file opening order, you should use a programmer-controlled open or use a CL program to open the files before calling the program.

If a program shares the open data path for a primary or secondary file, the program must process the detail calculations for the record being processed before calling another program that shares that open data path. Otherwise, if lookahead is used or if the call is at total time, sharing the open data path for a primary or secondary file may cause the called program to read data from the wrong record in the file.

You must make sure that when the shared file is opened for the first time, all of the open options that are required for subsequent opens of the file are specified. If the open options specified for subsequent opens of a shared file are not included in those specified for the first open of a shared file, an error message is sent to the program.

Table 1 details the system open options allowed for each of the open options you can specify.

Table 1. System Open Options Allowed with User Open Options
RPG User
Open Options
System
Open Options
INPUT INPUT
OUTPUT OUTPUT (program created file)
UPDATE INPUT, UPDATE, DELETE
ADD OUTPUT (existing file)

For additional information about sharing an open data path and activation group versus job level scope, see the ILE Concepts manual.