Skip to main content

Record Lock Info from SQL - IBM i

Record Locks

Record locks often causes the programs to crash if not handled properly. Identifying which job is locking the record is essential to resolve the issue (specially if the locking job is interactive idle for long time). 

One way of identifying the Record locks is by using the command DSPRCDLCK (Display Record Locks). 

This command accepts the below parameters and allows checking for the locks at the file, member and record level. Output of this command can only be either display or print. 

                       Display Record Locks (DSPRCDLCK)              
                                                                     
Type choices, press Enter.                                           
                                                                     
Physical file  . . . . . . . . . > TESTFILE      Name                
  Library  . . . . . . . . . . . >   PREDDY1     Name, *LIBL, *CURLIB
Member . . . . . . . . . . . . .   *FIRST        Name, *FIRST        
Record number  . . . . . . . . .   *ALL          1-4294967288, *ALL  
Output . . . . . . . . . . . . .   *             *, *PRINT           

Physical File - Name of the physical file for which record locks are to be checked. Locks can be checked only for one physical file at a time. 

Library - Library in which the physical file is present. Valid values are library name, *LIBL (default) and *CURLIB. 

Member - Name of the physical file member for which record locks are to be checked. Specific member name can be entered otherwise *FIRST (default) is considered. Record locks can be checked for only one member at a time. 

Record number - Record number can be entered for which we need to check if the specific record is locked. *ALL (default) checks for all the records in the file/member specified. 

Output - Output can either be displayed (by using * in Interactive job) or printed to spool file (by using * in Batch job or *PRINT in Interactive/Batch job).

This looks straight forward when we need to check the record locks for a single file or couple of files. And, If we need to resolve the locks manually. 

But, If we need to
  • Identify the record locks for a group of files.
  • Identify the record locks for all the members of a file. 
  • Identify the record locks for all the files in a Library.  
  • Identify the record locks by a specific Job. 
This can be easily achieved from SQL by running a query on the view RECORD_LOCK_INFO in QSYS2 (System name - RCD_LOCK). 

E.g.: 

Identify the record locks for a group of files. 

SELECT * FROM QSYS2/RECORD_LOCK_INFO WHERE TABLE_NAME IN ('TESTFILE', 'TESTFILE1', 'TESTFILE2') ;

Identify the record locks for all members of a file - Just querying for a file name (and library) would return the locks present in all members of the file. 

SELECT * FROM QSYS2/RECORD_LOCK_INFO WHERE TABLE_SCHEMA = 'PREDDY1' AND TABLE_NAME = 'TESTFILE' ;

Identify the record locks for all the files in a Library. 

SELECT * FROM QSYS2/RECORD_LOCK_INFO WHERE TABLE_SCHEMA = 'PREDDY1'  ;

Identify the record locks by a specific Job. 

SELECT * FROM QSYS2/RECORD_LOCK_INFO WHERE JOB_NAME = '123456/PREDDY/QPADEV1234' ;

Apart from these, the query can be run based on any of the columns available in the view RECORD_LOCK_INFO. 

Below are the columns present in the view. 

TABLE_SCHEMA (TABSCHEMA) - Name of the schema.

TABLE_NAME (TABNAME) - Name of the table.

TABLE_PARTITION (TABPART) - Name of the table partition or member that contains the locked record.

SYSTEM_TABLE_SCHEMA (SYS_DNAME) - System name of the schema.

SYSTEM_TABLE_NAME (SYS_TNAME) - System name of the table

SYSTEM_TABLE_MEMBER (SYS_MNAME) - The name of the member that contains the locked record.

RELATIVE_RECORD_NUMBER (RRN) - The relative record number (RRN) of the record that is locked.

LOCK_STATE (LOCK_STATE) - The lock condition for the record. Valid values are READ (The record is locked for read. Another job may read the same record but cannot lock the record for update intent. The record cannot be changed by another job as long as one job holds a read lock on the record.), UPDATE (The record is locked for update intent. Another job may read the record but may not obtain a read or update lock on it until the lock is released.) and INTERNAL (The row is locked internally for read. For a short time the operating system holds an internal lock to access the row. Another job may read the same row and may even have the row locked for update intent. However, if another job does have the row locked for update intent, the actual change of the row will not proceed until the internal lock is released.)

LOCK_STATUS (STATUS) - The status of the lock. Valid values are HELD (The lock is currently held by the job.), WAITING (The job is waiting for the lock.)

LOCK_SCOPE (LOCK_SCOPE) - The scope of the lock. Valid values are JOB, THREAD and LOCK SPACE

JOB_NAME (JOB_NAME) - The qualified job name.

THREAD_ID (THREAD_ID) - The thread that is associated with the lock. If a held lock is job scoped, returns the null value. If a held lock is thread scoped, contains the identifier for the thread holding the lock. If the scope of the lock is to the lock space and the lock is not held, contains the identifier of the thread requesting the lock. If the lock is requested but not yet available, contains the identifier of the thread requesting the lock.

LOCK_SPACE_ID (LOCKID) - When the LOCK_SCOPE column value is LOCK SPACE and the lock is being waited on by a thread, contains the lock space ID value for which the lock is being waited on. Otherwise, contains the null value.

If you have any Suggestions or Feedback, Please leave a comment below or use Contact Form. 

Comments

Popular posts from this blog

All about READ in RPGLE & Why we use it with SETLL/SETGT?

READ READ is one of the most used Opcodes in RPGLE. As the name suggests main purpose of this Opcode is to read a record from Database file. What are the different READ Opcodes? To list, Below are the five Opcodes.  READ - Read a Record READC - Read Next Changed Record READE - Read Equal Key Record READP - Read Prior Record READPE - Read Prior Equal Record We will see more about each of these later in this article. Before that, We will see a bit about SETLL/SETGT .  SETLL (Set Lower Limit) SETLL accepts Key Fields or Relative Record Number (RRN) as Search Arguments and positions the file at the Corresponding Record (or Next Record if exact match isn't found).  SETGT (Set Greater Than) SETGT accepts Key Fields or Relative Record Number (RRN) as Search Arguments and positions the file at the Next Record (Greater Than the Key value). Syntax: SETLL SEARCH-ARGUMENTS/KEYFIELDS FILENAME SETGT  SEARCH-ARGUMENTS/KEYFIELDS FILENAME One of the below can be passed as Search Arguments. Key Fiel

What we need to know about CHAIN (RPGLE) & How is it different from READ?

CHAIN READ & CHAIN, These are one of the most used (& useful) Opcodes by any RPG developer. These Opcodes are used to read a record from file. So, What's the difference between CHAIN & READ?   CHAIN operation retrieves a record based on the Key specified. It's more like Retrieving Random record from a Database file based on the Key fields.  READ operation reads the record currently pointed to from a Database file. There are multiple Opcodes that start with READ and all are used to read a record but with slight difference. We will see more about different Opcodes and How they are different from each other (and CHAIN) in another article. Few differences to note.  CHAIN requires Key fields to read a record where as READ would read the record currently pointed to (SETLL or SETGT are used to point a Record).  If there are multiple records with the same Key data, CHAIN would return the same record every time. READE can be used to read all the records with the specified Ke

Extract a portion of a Date/Time/Timestamp in RPGLE - IBM i

%SUBDT Extracting Year, Month, Day, Hour, Minutes, Seconds or Milli seconds of a given Date/Time/Timestamp is required most of the times.  This can be extracted easily by using %SUBDT. BIF name looks more similar to %SUBST which is used to extract a portion of string by passing from and two positions of the original string. Instead, We would need to pass a value (i.e., Date, Time or Timestamp ) and Unit (i.e., *YEARS, *MONTHS, *DAYS, *HOURS, *MINUTES, *SECONDS or *MSECONDS) to %SUBDT.  Valid unit should be passed for the type of the value passed. Below are the valid values for each type. Date - *DAYS, *MONTHS, *YEARS Time - *HOURS, *MINUTES, *SECONDS Timestamp - *DAYS, *MONTHS, *YEARS, *HOURS, *MINUTES, *SECONDS, *MSECONDS Syntax: %SUBDT(value : unit { : digits { : decpos} }) Value and Unit are the mandatory arguments.  Digits and Decimal positions are optional and can only be used with *SECONDS for Timestamp. We can either pass the full form for the unit or use the short form. Below i