MainframeMaster
MainframeMaster

COBOL Tutorial

Progress0 of 0 lessons

COBOL Transaction Processing

Transaction processing in COBOL involves processing transaction records to update master files, generate reports, and maintain data integrity. This typically includes reading transaction files, matching transactions to master records, applying updates (adds, changes, deletes), handling control breaks, validating transactions, and maintaining audit trails. Understanding transaction processing is essential for building batch processing systems, maintaining master data, and implementing business logic that processes business events in mainframe COBOL applications.

What is Transaction Processing?

Transaction processing involves:

  • Reading transactions: Processing transaction records from transaction files
  • Matching to masters: Finding corresponding master records
  • Applying updates: Modifying master records based on transactions
  • Validating data: Ensuring transactions are valid before processing
  • Handling errors: Managing invalid transactions and errors
  • Generating reports: Creating reports of processing results
  • Maintaining audit trails: Recording what was processed

Transaction processing is fundamental to batch processing systems where transactions are collected and processed together.

Master Files and Transaction Files

Understanding the relationship between master and transaction files:

Master File vs Transaction File
AspectMaster FileTransaction File
PurposeContains current, authoritative dataContains business events/changes to process
OrganizationTypically indexed (KSDS) for direct accessTypically sequential, processed in order
Update frequencyUpdated by transaction processingCreated by business events
Record typeMaster records (accounts, items, etc.)Transaction records (sales, payments, etc.)
AccessRandom access by keySequential access
LifecyclePersistent, maintained over timeProcessed and archived

Transaction Types

Transactions typically have types that determine the operation:

Common Transaction Types

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
WORKING-STORAGE SECTION. 01 TRANSACTION-TYPES. 05 TRANS-ADD PIC X VALUE 'A'. *> Add new master record 05 TRANS-UPDATE PIC X VALUE 'U'. *> Update existing master 05 TRANS-DELETE PIC X VALUE 'D'. *> Delete master record 05 TRANS-QUERY PIC X VALUE 'Q'. *> Query only (no update) 01 TRANSACTION-RECORD. 05 TRANS-TYPE PIC X. 05 TRANS-KEY PIC 9(5). 05 TRANS-DATA PIC X(50). 05 TRANS-AMOUNT PIC 9(8)V99. 05 TRANS-DATE PIC 9(8).

Transaction types determine what operation to perform on the master file.

Basic Transaction Processing Pattern

The standard transaction processing pattern:

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
PROCEDURE DIVISION. MAIN-PARA. PERFORM INITIALIZATION PERFORM PROCESS-TRANSACTIONS PERFORM FINALIZATION STOP RUN. INITIALIZATION. OPEN INPUT TRANSACTION-FILE OPEN I-O MASTER-FILE OPEN OUTPUT ERROR-FILE OPEN OUTPUT REPORT-FILE *> Initialize counters and totals MOVE ZERO TO TRANS-COUNT MOVE ZERO TO SUCCESS-COUNT MOVE ZERO TO ERROR-COUNT. PROCESS-TRANSACTIONS. PERFORM UNTIL END-OF-TRANSACTIONS READ TRANSACTION-FILE AT END SET END-OF-TRANSACTIONS TO TRUE NOT AT END ADD 1 TO TRANS-COUNT PERFORM PROCESS-ONE-TRANSACTION END-READ END-PERFORM. PROCESS-ONE-TRANSACTION. *> Validate transaction PERFORM VALIDATE-TRANSACTION IF TRANSACTION-VALID *> Process based on type EVALUATE TRANS-TYPE WHEN 'A' PERFORM ADD-MASTER-RECORD WHEN 'U' PERFORM UPDATE-MASTER-RECORD WHEN 'D' PERFORM DELETE-MASTER-RECORD WHEN OTHER PERFORM HANDLE-INVALID-TYPE END-EVALUATE ELSE PERFORM WRITE-ERROR-RECORD END-IF. FINALIZATION. *> Write summary report PERFORM WRITE-SUMMARY-REPORT CLOSE TRANSACTION-FILE MASTER-FILE ERROR-FILE REPORT-FILE.

Processing Different Transaction Types

Adding a Master Record

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
ADD-MASTER-RECORD. *> Check if master already exists MOVE TRANS-KEY TO MASTER-KEY READ MASTER-FILE INVALID KEY *> Master doesn't exist - good for add MOVE TRANS-KEY TO MASTER-KEY MOVE TRANS-DATA TO MASTER-DATA MOVE TRANS-AMOUNT TO MASTER-BALANCE MOVE TRANS-DATE TO MASTER-DATE WRITE MASTER-RECORD INVALID KEY DISPLAY "Error adding master: " MASTER-KEY ADD 1 TO ERROR-COUNT NOT INVALID KEY ADD 1 TO SUCCESS-COUNT DISPLAY "Master added: " MASTER-KEY END-WRITE NOT INVALID KEY *> Master already exists - error DISPLAY "ERROR: Master already exists: " MASTER-KEY MOVE "Duplicate key on add" TO ERROR-MESSAGE PERFORM WRITE-ERROR-RECORD END-READ.

Updating a Master Record

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
UPDATE-MASTER-RECORD. *> Read master record to update MOVE TRANS-KEY TO MASTER-KEY READ MASTER-FILE INVALID KEY *> Master doesn't exist - error DISPLAY "ERROR: Master not found for update: " MASTER-KEY MOVE "Master not found" TO ERROR-MESSAGE PERFORM WRITE-ERROR-RECORD NOT INVALID KEY *> Master exists - apply update ADD TRANS-AMOUNT TO MASTER-BALANCE MOVE TRANS-DATE TO MASTER-LAST-UPDATE REWRITE MASTER-RECORD INVALID KEY DISPLAY "Error updating master: " MASTER-KEY ADD 1 TO ERROR-COUNT NOT INVALID KEY ADD 1 TO SUCCESS-COUNT DISPLAY "Master updated: " MASTER-KEY END-REWRITE END-READ.

Deleting a Master Record

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
DELETE-MASTER-RECORD. *> Read master to verify it exists MOVE TRANS-KEY TO MASTER-KEY READ MASTER-FILE INVALID KEY *> Master doesn't exist - error DISPLAY "ERROR: Master not found for delete: " MASTER-KEY MOVE "Master not found" TO ERROR-MESSAGE PERFORM WRITE-ERROR-RECORD NOT INVALID KEY *> Master exists - delete it DELETE MASTER-FILE RECORD INVALID KEY DISPLAY "Error deleting master: " MASTER-KEY ADD 1 TO ERROR-COUNT NOT INVALID KEY ADD 1 TO SUCCESS-COUNT DISPLAY "Master deleted: " MASTER-KEY END-DELETE END-READ.

Transaction Validation

Validating transactions before processing:

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
VALIDATE-TRANSACTION. MOVE 'Y' TO TRANSACTION-VALID *> Validate transaction type IF TRANS-TYPE NOT = 'A' AND TRANS-TYPE NOT = 'U' AND TRANS-TYPE NOT = 'D' MOVE 'N' TO TRANSACTION-VALID MOVE "Invalid transaction type" TO ERROR-MESSAGE END-IF *> Validate key is present IF TRANS-KEY = ZERO OR TRANS-KEY = SPACES MOVE 'N' TO TRANSACTION-VALID MOVE "Missing or invalid key" TO ERROR-MESSAGE END-IF *> Validate amount (if applicable) IF TRANS-AMOUNT < 0 MOVE 'N' TO TRANSACTION-VALID MOVE "Invalid amount" TO ERROR-MESSAGE END-IF *> Validate date format IF TRANS-DATE NOT NUMERIC MOVE 'N' TO TRANSACTION-VALID MOVE "Invalid date format" TO ERROR-MESSAGE END-IF *> Business rule validation IF TRANS-TYPE = 'U' *> For updates, verify master exists MOVE TRANS-KEY TO MASTER-KEY READ MASTER-FILE INVALID KEY MOVE 'N' TO TRANSACTION-VALID MOVE "Master not found for update" TO ERROR-MESSAGE END-READ END-IF.

Control Breaks in Transaction Processing

Control breaks group transactions and generate subtotals:

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
WORKING-STORAGE SECTION. 01 PREV-CUSTOMER-ID PIC 9(5) VALUE ZERO. 01 CURRENT-CUSTOMER-ID PIC 9(5). 01 CUSTOMER-TOTAL PIC 9(10)V99 VALUE ZERO. 01 GRAND-TOTAL PIC 9(12)V99 VALUE ZERO. PROCEDURE DIVISION. PROCESS-WITH-CONTROL-BREAKS. PERFORM UNTIL END-OF-TRANSACTIONS READ TRANSACTION-FILE AT END SET END-OF-TRANSACTIONS TO TRUE NOT AT END MOVE TRANS-CUSTOMER-ID TO CURRENT-CUSTOMER-ID *> Check for control break IF PREV-CUSTOMER-ID NOT = ZERO AND CURRENT-CUSTOMER-ID NOT = PREV-CUSTOMER-ID *> Control break - process previous group PERFORM PROCESS-CUSTOMER-BREAK END-IF *> Process current transaction ADD TRANS-AMOUNT TO CUSTOMER-TOTAL ADD TRANS-AMOUNT TO GRAND-TOTAL MOVE CURRENT-CUSTOMER-ID TO PREV-CUSTOMER-ID END-READ END-PERFORM *> Process final group IF PREV-CUSTOMER-ID NOT = ZERO PERFORM PROCESS-CUSTOMER-BREAK END-IF *> Process grand total PERFORM PROCESS-GRAND-TOTAL. PROCESS-CUSTOMER-BREAK. *> Write customer subtotal DISPLAY "Customer " PREV-CUSTOMER-ID " Total: " CUSTOMER-TOTAL WRITE REPORT-LINE FROM CUSTOMER-SUBTOTAL-LINE *> Reset for next customer MOVE ZERO TO CUSTOMER-TOTAL.

Error Handling in Transaction Processing

Proper error handling is crucial:

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
WRITE-ERROR-RECORD. ADD 1 TO ERROR-COUNT *> Write to error file MOVE TRANSACTION-RECORD TO ERROR-RECORD MOVE ERROR-MESSAGE TO ERROR-DETAIL MOVE FUNCTION CURRENT-DATE TO ERROR-TIMESTAMP WRITE ERROR-RECORD *> Write to error report WRITE REPORT-LINE FROM ERROR-LINE DISPLAY "ERROR: " ERROR-MESSAGE DISPLAY " Transaction: " TRANS-KEY.

Batch Processing Example

Complete batch transaction processing example:

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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
IDENTIFICATION DIVISION. PROGRAM-ID. BATCH-TRANSACTION-PROCESSOR. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT TRANSACTION-FILE ASSIGN TO TRANS.DAT ORGANIZATION IS SEQUENTIAL. SELECT MASTER-FILE ASSIGN TO MASTER.VSAM ORGANIZATION IS INDEXED ACCESS MODE IS RANDOM RECORD KEY IS CUSTOMER-ID FILE STATUS IS MASTER-STATUS. SELECT ERROR-FILE ASSIGN TO ERRORS.DAT ORGANIZATION IS SEQUENTIAL. DATA DIVISION. FILE SECTION. FD TRANSACTION-FILE. 01 TRANSACTION-RECORD. 05 TRANS-TYPE PIC X. 05 TRANS-CUSTOMER-ID PIC 9(5). 05 TRANS-AMOUNT PIC 9(8)V99. 05 TRANS-DESCRIPTION PIC X(30). FD MASTER-FILE. 01 MASTER-RECORD. 05 CUSTOMER-ID PIC 9(5). 05 CUSTOMER-NAME PIC X(30). 05 CUSTOMER-BALANCE PIC 9(8)V99. 05 LAST-UPDATE-DATE PIC 9(8). FD ERROR-FILE. 01 ERROR-RECORD PIC X(100). WORKING-STORAGE SECTION. 01 FILE-STATUS-FIELDS. 05 MASTER-STATUS PIC XX. 05 END-OF-TRANS PIC X VALUE 'N'. 88 END-OF-TRANSACTIONS VALUE 'Y'. 01 COUNTERS. 05 TRANS-COUNT PIC 9(6) VALUE ZERO. 05 SUCCESS-COUNT PIC 9(6) VALUE ZERO. 05 ERROR-COUNT PIC 9(6) VALUE ZERO. 01 VALIDATION-FLAGS. 05 TRANSACTION-VALID PIC X VALUE 'Y'. 05 ERROR-MESSAGE PIC X(50). PROCEDURE DIVISION. MAIN-PARA. DISPLAY "=== BATCH TRANSACTION PROCESSOR ===" PERFORM INITIALIZATION PERFORM PROCESS-TRANSACTIONS PERFORM FINALIZATION DISPLAY "=== PROCESSING COMPLETE ===" STOP RUN. INITIALIZATION. DISPLAY "Initializing batch processing..." OPEN INPUT TRANSACTION-FILE OPEN I-O MASTER-FILE OPEN OUTPUT ERROR-FILE IF MASTER-STATUS NOT = '00' DISPLAY "ERROR: Cannot open master file: " MASTER-STATUS STOP RUN END-IF MOVE ZERO TO TRANS-COUNT MOVE ZERO TO SUCCESS-COUNT MOVE ZERO TO ERROR-COUNT DISPLAY "Initialization complete". PROCESS-TRANSACTIONS. DISPLAY "Processing transactions..." PERFORM UNTIL END-OF-TRANSACTIONS READ TRANSACTION-FILE AT END SET END-OF-TRANSACTIONS TO TRUE NOT AT END ADD 1 TO TRANS-COUNT PERFORM PROCESS-ONE-TRANSACTION END-READ END-PERFORM DISPLAY "Transactions processed: " TRANS-COUNT. PROCESS-ONE-TRANSACTION. *> Validate transaction PERFORM VALIDATE-TRANSACTION IF TRANSACTION-VALID *> Process based on type EVALUATE TRANS-TYPE WHEN 'A' PERFORM ADD-MASTER-RECORD WHEN 'U' PERFORM UPDATE-MASTER-RECORD WHEN 'D' PERFORM DELETE-MASTER-RECORD WHEN OTHER MOVE "Invalid transaction type" TO ERROR-MESSAGE PERFORM WRITE-ERROR-RECORD END-EVALUATE ELSE PERFORM WRITE-ERROR-RECORD END-IF. VALIDATE-TRANSACTION. MOVE 'Y' TO TRANSACTION-VALID *> Validate transaction type IF TRANS-TYPE NOT = 'A' AND TRANS-TYPE NOT = 'U' AND TRANS-TYPE NOT = 'D' MOVE 'N' TO TRANSACTION-VALID MOVE "Invalid transaction type" TO ERROR-MESSAGE END-IF *> Validate customer ID IF TRANS-CUSTOMER-ID = ZERO MOVE 'N' TO TRANSACTION-VALID MOVE "Invalid customer ID" TO ERROR-MESSAGE END-IF. ADD-MASTER-RECORD. MOVE TRANS-CUSTOMER-ID TO CUSTOMER-ID READ MASTER-FILE INVALID KEY *> Good - master doesn't exist MOVE TRANS-CUSTOMER-ID TO CUSTOMER-ID MOVE SPACES TO CUSTOMER-NAME MOVE TRANS-AMOUNT TO CUSTOMER-BALANCE ACCEPT LAST-UPDATE-DATE FROM DATE YYYYMMDD WRITE MASTER-RECORD INVALID KEY ADD 1 TO ERROR-COUNT NOT INVALID KEY ADD 1 TO SUCCESS-COUNT END-WRITE NOT INVALID KEY MOVE "Master already exists" TO ERROR-MESSAGE PERFORM WRITE-ERROR-RECORD END-READ. UPDATE-MASTER-RECORD. MOVE TRANS-CUSTOMER-ID TO CUSTOMER-ID READ MASTER-FILE INVALID KEY MOVE "Master not found" TO ERROR-MESSAGE PERFORM WRITE-ERROR-RECORD NOT INVALID KEY ADD TRANS-AMOUNT TO CUSTOMER-BALANCE ACCEPT LAST-UPDATE-DATE FROM DATE YYYYMMDD REWRITE MASTER-RECORD INVALID KEY ADD 1 TO ERROR-COUNT NOT INVALID KEY ADD 1 TO SUCCESS-COUNT END-REWRITE END-READ. DELETE-MASTER-RECORD. MOVE TRANS-CUSTOMER-ID TO CUSTOMER-ID DELETE MASTER-FILE RECORD INVALID KEY MOVE "Master not found" TO ERROR-MESSAGE PERFORM WRITE-ERROR-RECORD NOT INVALID KEY ADD 1 TO SUCCESS-COUNT END-DELETE. WRITE-ERROR-RECORD. ADD 1 TO ERROR-COUNT MOVE TRANSACTION-RECORD TO ERROR-RECORD WRITE ERROR-RECORD DISPLAY "ERROR: " ERROR-MESSAGE " - Customer: " TRANS-CUSTOMER-ID. FINALIZATION. DISPLAY "Finalizing batch processing..." DISPLAY "Total transactions: " TRANS-COUNT DISPLAY "Successful: " SUCCESS-COUNT DISPLAY "Errors: " ERROR-COUNT CLOSE TRANSACTION-FILE MASTER-FILE ERROR-FILE DISPLAY "Finalization complete".

Best Practices for Transaction Processing

Follow these best practices:

  • Validate all transactions: Check transaction data before processing
  • Handle all transaction types: Support ADD, UPDATE, DELETE, and any other types
  • Check master existence: Verify master exists for updates/deletes, doesn't exist for adds
  • Maintain error files: Write invalid transactions to error files for review
  • Track processing statistics: Count transactions, successes, and errors
  • Generate summary reports: Provide reports of processing results
  • Handle file status codes: Check file status after all I/O operations
  • Use appropriate file organization: Indexed files for masters, sequential for transactions
  • Process in correct order: Sort transactions if needed for control breaks
  • Maintain audit trails: Log what was processed and when

Explain Like I'm 5: Transaction Processing

Think of transaction processing like managing a piggy bank:

  • Master file is like your piggy bank - it has your current balance written on it
  • Transaction file is like a list of things that happened - you got $5, spent $2, got $10
  • Processing transactions is like going through your list and updating your piggy bank balance for each thing that happened
  • Validation is like checking that each transaction makes sense before you update the balance
  • Error file is like writing down transactions that don't make sense so you can fix them later

So transaction processing is like keeping your piggy bank balance up to date by going through a list of things that happened and updating the balance for each one!

Practice Exercises

Complete these exercises to reinforce your understanding:

Exercise 1: Basic Transaction Processing

Create a program that reads transaction records, validates them, and updates a master file. Handle ADD, UPDATE, and DELETE transaction types.

Exercise 2: Transaction Validation

Create a comprehensive validation routine that checks transaction type, key fields, data formats, and business rules. Write invalid transactions to an error file.

Exercise 3: Control Break Processing

Create a program that processes transactions with control breaks by customer ID, generating subtotals for each customer and a grand total.

Exercise 4: Batch Processing with Reporting

Create a batch processing program that processes transactions, updates master files, writes errors to an error file, and generates a summary report of processing results.

Exercise 5: Error Recovery

Create a program that processes transactions, handles errors gracefully, maintains statistics, and provides detailed error reporting with transaction details.

Test Your Knowledge

1. What is a master file in transaction processing?

  • A file containing transactions
  • A file containing current, authoritative data records
  • A file containing error records
  • A temporary file

2. What is a transaction file?

  • A file containing master records
  • A file containing business events or changes to be processed
  • A file containing reports
  • A file containing errors only

3. What is a control break?

  • An error in processing
  • A change in a control field value during sequential processing
  • A file error
  • A program termination

4. What is the typical sequence for updating a master file?

  • WRITE, then READ
  • READ transaction, READ master, update, REWRITE master
  • DELETE, then WRITE
  • REWRITE, then READ

5. What is batch processing?

  • Processing one transaction at a time
  • Collecting transactions and processing them together
  • Processing only errors
  • Processing in real-time

6. What should you do with invalid transactions?

  • Ignore them
  • Process them anyway
  • Write them to an error file or report for review
  • Delete them

Related Concepts

Related Pages