Reading data area in RPGLE - Update and Locking issues

Data area comes handy when storing application or program specific information. If you define UDS in columns 23-25 when defining the data area then the program automatically locks the data area for you when the program starts and unlock it when the program ends. You can also specify unlock to unlock it, but in the mean time any other program cannot access the data area. Here is an example code.
d $data          uds                  dtaara(@MYDATA)                    
d  $value                       50a                                      
                                                                         
 /free                                                                   
                                                                         
        dsply $value;     // Display data area value                     
                                                                         
      In $data;           // Data Area is locked                         
        dsply $value;     // Display data area value                     
      Out $data;          // Update and unlock the data area             
                                                                         
      In $data;           // Reads but doesn't locks the data area       
        dsply $value;     // Display data area value                     
      Unlock $data;       // Unlock the data area no affect              
                                                                         
      In *lock $data;     // Reads and Locks the data area               
        dsply $value;     // Display data area value                     
      Unlock $data;       // Unlock the data area                        
                                                                         
      *inlr = *on;                                                       
      return;                                                             

/end-free  

Its better to not define the UDS and lock the data area when needed and unlock after update or when unlock is specified. Solves a lot of issues with Message Id: RNQ0412 - Data area *LIBL/{your_data_area) is not allocated for output. Here is an example code.
d $data           ds                  dtaara(@MYDATA)       
d  $value                       50a                         
                                                            
 /free                                                      
                                                            
      In $data;           // Just Read, don't lock it       
        dsply $value;     // Display data area value        
                                                            
      In *lock $data;     // Reads and Locks the data area  
        dsply $value;     // Display data area value        
      Out $data;          // Update and unlock the data area
                                                            
      In *lock $data;     // Reads and Locks the data area  
        dsply $value;     // Display data area value        
      Unlock $data;       // Unlock the data area           
                                                            
      *inlr = *on;                                          
      return;                                               
                                                            
 /end-free       

No comments:

Post a Comment

NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.