Q. What is the best way using D-specs to define a data area, where I want to get the value from the data area and never lock the data area? We use a data area to hold an encryption key. Other programmers are using the old *DTAARA DEFINE, directly followed by an UNLOCK because they say the D-specs can’t handle it without locking the data area. Is there a better way?
A. When you use UDS in columns 23-25 to define a data area data structure, the RPG program will read and lock the data area automatically when the program starts, then upate and lock it when the program ends.
You can use the IN, OUT, and UNLOCK operations to specify additional operations, but the data area will be locked when the program begins:
D Mydta UDS DTAARA('MYDTAARA') ... (Subfields) /Free Unlock Mydta; // Unlock MYDTAARA /End-free
If you want to read a data area without locking it, omit the U from column 23, but use the DTAARA keyword on the definition. Then, explicitly control the data area, using the IN and OUT operations to read and update the data area:
D Lda DS DTAARA(*LDA) ... (Subfields) D Mydta DS DTAARA('MYDTAARA') ... (Subfields) /Free In Lda; // Read the LDA without locking it In Mydta; // Read MYDTAARA without locking it In *Lock Mydta; // Read and lock MYDTAARA Out Mydta; // Update and unlock MYDTAARA In *Lock Mydta; // Read and lock MYDTAARA Unlock Mydta; // Unlock MYDTAARA /End-free