MainframeMaster

COBOL Tutorial

Progress0 of 0 lessons

COBOL UNLOCK Statement

The UNLOCK statement is used for releasing file locks in multi-user environments.

Overview

The UNLOCK statement provides a mechanism for releasing file locks that were previously established on file records. This is essential in multi-user environments where multiple programs or users need concurrent access to the same files.

When a file record is locked, other users or processes are prevented from accessing that record until the lock is released. The UNLOCK statement allows you to explicitly release these locks when they are no longer needed.

Syntax

cobol
1
UNLOCK file-name RECORD

Basic syntax for the UNLOCK statement. The file-name specifies which file to unlock, and the RECORD keyword indicates that the current record should be unlocked.

Parameters

  • file-name: The name of the file from which to release locks
  • RECORD: Optional keyword indicating that the current record should be unlocked

Practical Examples

Example 1: Basic Record Unlocking

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
IDENTIFICATION DIVISION. PROGRAM-ID. UNLOCK-EXAMPLE. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT CUSTOMER-FILE ASSIGN TO "CUSTOMER.DAT" ORGANIZATION IS INDEXED ACCESS MODE IS RANDOM RECORD KEY IS CUSTOMER-ID. DATA DIVISION. FILE SECTION. FD CUSTOMER-FILE. 01 CUSTOMER-RECORD. 05 CUSTOMER-ID PIC 9(6). 05 CUSTOMER-NAME PIC X(30). 05 CUSTOMER-BALANCE PIC 9(8)V99. WORKING-STORAGE SECTION. 01 WS-CUSTOMER-ID PIC 9(6) VALUE 123456. PROCEDURE DIVISION. MAIN-PROCESS. OPEN I-O CUSTOMER-FILE * Read and lock a record READ CUSTOMER-FILE INVALID KEY DISPLAY "Customer not found" NOT INVALID KEY * Process the record DISPLAY "Processing customer: " CUSTOMER-NAME * Update the record ADD 100 TO CUSTOMER-BALANCE REWRITE CUSTOMER-RECORD * Unlock the record UNLOCK CUSTOMER-FILE RECORD END-READ CLOSE CUSTOMER-FILE STOP RUN.

This example demonstrates basic record unlocking. The program reads a customer record, updates the balance, rewrites the record, and then unlocks it to allow other users access. The UNLOCK statement releases the lock that was established when the record was read.

Example 2: Multi-User File Access

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
IDENTIFICATION DIVISION. PROGRAM-ID. MULTI-USER-ACCESS. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT INVENTORY-FILE ASSIGN TO "INVENTORY.DAT" ORGANIZATION IS INDEXED ACCESS MODE IS RANDOM RECORD KEY IS PRODUCT-ID. DATA DIVISION. FILE SECTION. FD INVENTORY-FILE. 01 INVENTORY-RECORD. 05 PRODUCT-ID PIC 9(6). 05 PRODUCT-NAME PIC X(30). 05 QUANTITY-ON-HAND PIC 9(5). WORKING-STORAGE SECTION. 01 WS-PRODUCT-ID PIC 9(6). 01 WS-QUANTITY-TO-ADD PIC 9(5). PROCEDURE DIVISION. UPDATE-INVENTORY. DISPLAY "Enter product ID: " ACCEPT WS-PRODUCT-ID DISPLAY "Enter quantity to add: " ACCEPT WS-QUANTITY-TO-ADD OPEN I-O INVENTORY-FILE * Read the product record MOVE WS-PRODUCT-ID TO PRODUCT-ID READ INVENTORY-FILE INVALID KEY DISPLAY "Product not found" CLOSE INVENTORY-FILE STOP RUN NOT INVALID KEY * Update quantity ADD WS-QUANTITY-TO-ADD TO QUANTITY-ON-HAND * Rewrite the record REWRITE INVENTORY-RECORD * Unlock to allow other users access UNLOCK INVENTORY-FILE RECORD DISPLAY "Inventory updated successfully" END-READ CLOSE INVENTORY-FILE STOP RUN.

This example shows how UNLOCK is used in a multi-user inventory system. After updating the inventory quantity, the record is unlocked to allow other users to access the same product record. This prevents conflicts when multiple users are updating inventory simultaneously.

Example 3: Error Handling with UNLOCK

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
IDENTIFICATION DIVISION. PROGRAM-ID. UNLOCK-ERROR-HANDLING. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT ACCOUNT-FILE ASSIGN TO "ACCOUNT.DAT" ORGANIZATION IS INDEXED ACCESS MODE IS RANDOM RECORD KEY IS ACCOUNT-NUMBER. DATA DIVISION. FILE SECTION. FD ACCOUNT-FILE. 01 ACCOUNT-RECORD. 05 ACCOUNT-NUMBER PIC 9(10). 05 ACCOUNT-BALANCE PIC 9(10)V99. 05 ACCOUNT-STATUS PIC X(1). WORKING-STORAGE SECTION. 01 WS-ACCOUNT-NUMBER PIC 9(10). 01 WS-WITHDRAWAL-AMOUNT PIC 9(8)V99. PROCEDURE DIVISION. PROCESS-WITHDRAWAL. DISPLAY "Enter account number: " ACCEPT WS-ACCOUNT-NUMBER DISPLAY "Enter withdrawal amount: " ACCEPT WS-WITHDRAWAL-AMOUNT OPEN I-O ACCOUNT-FILE * Read the account record MOVE WS-ACCOUNT-NUMBER TO ACCOUNT-NUMBER READ ACCOUNT-FILE INVALID KEY DISPLAY "Account not found" CLOSE ACCOUNT-FILE STOP RUN NOT INVALID KEY * Check if withdrawal is possible IF ACCOUNT-BALANCE >= WS-WITHDRAWAL-AMOUNT * Process withdrawal SUBTRACT WS-WITHDRAWAL-AMOUNT FROM ACCOUNT-BALANCE * Update the record REWRITE ACCOUNT-RECORD * Unlock the record UNLOCK ACCOUNT-FILE RECORD DISPLAY "Withdrawal processed successfully" ELSE * Insufficient funds - unlock without updating UNLOCK ACCOUNT-FILE RECORD DISPLAY "Insufficient funds" END-IF END-READ CLOSE ACCOUNT-FILE STOP RUN.

This example demonstrates error handling with UNLOCK. Even when there are insufficient funds and no update is made, the record is still unlocked to release the lock. This ensures that other users can access the account record even when the current operation fails.

Best Practices and Considerations

Best Practices

  • Always unlock records when you are finished processing them
  • Unlock records even when errors occur to prevent deadlocks
  • Use UNLOCK in conjunction with proper error handling
  • Consider the timing of UNLOCK operations in your application logic
  • Test UNLOCK behavior in multi-user scenarios

Considerations

  • File organization and access mode compatibility
  • Multi-user environment requirements
  • Performance implications of locking and unlocking
  • Error handling and recovery procedures
  • Deadlock prevention strategies

Test Your Knowledge

1. What is the primary purpose of the UNLOCK statement in COBOL?

  • To unlock program execution
  • To release file locks in multi-user environments
  • To unlock memory resources
  • To unlock database connections

2. In which COBOL division is the UNLOCK statement typically used?

  • IDENTIFICATION DIVISION
  • ENVIRONMENT DIVISION
  • DATA DIVISION
  • PROCEDURE DIVISION

3. What is the relationship between UNLOCK and file sharing?

  • They are unrelated
  • UNLOCK enables file sharing by releasing locks
  • File sharing enables UNLOCK
  • They are the same thing

4. When should you use the UNLOCK statement?

  • Always after every file operation
  • When you want to release file locks to allow other users access
  • Only when closing files
  • Only for error conditions

5. What happens when an UNLOCK statement is executed?

  • The file is closed
  • File locks are released, allowing other users access
  • The program stops
  • Memory is freed

Frequently Asked Questions