COBOL COMMIT
The COMMIT statement in COBOL is used for transaction control and ensuring data integrity in database operations.
Overview and Purpose
COMMIT is a crucial statement for maintaining data integrity in COBOL applications that work with databases. It marks the successful completion of a transaction by permanently saving all changes made since the last COMMIT or transaction start. This ensures that related data modifications are treated as a single atomic unit, preventing partial updates that could leave the database in an inconsistent state. Understanding when and how to use COMMIT is essential for building reliable, enterprise-grade applications.
Basic COMMIT Usage
123456789EXEC SQL UPDATE CUSTOMER SET BALANCE = BALANCE - :WS-AMOUNT WHERE CUSTOMER-ID = :WS-CUSTOMER-ID END-EXEC EXEC SQL COMMIT END-EXEC
This example shows a basic COMMIT operation after updating a customer balance. The UPDATE statement modifies the database, but the changes remain temporary until the COMMIT is executed. Once COMMIT is processed, the balance change becomes permanent and visible to other programs. This pattern ensures that the database update is properly saved and won't be lost due to system failures or program termination.
Transaction Processing with Multiple Operations
12345678910111213141516171819202122EXEC SQL INSERT INTO TRANSACTION_LOG VALUES (:WS-TRANS-ID, :WS-CUSTOMER-ID, :WS-AMOUNT, :WS-DATE) END-EXEC EXEC SQL UPDATE ACCOUNT_BALANCE SET CURRENT_BALANCE = CURRENT_BALANCE + :WS-AMOUNT WHERE ACCOUNT-ID = :WS-ACCOUNT-ID END-EXEC IF SQLCODE = 0 EXEC SQL COMMIT END-EXEC DISPLAY "Transaction completed successfully" ELSE EXEC SQL ROLLBACK END-EXEC DISPLAY "Transaction failed - changes rolled back" END-IF
This example demonstrates transaction processing with multiple related operations. The program inserts a transaction log entry and updates an account balance as part of a single logical transaction. If both operations succeed (SQLCODE = 0), COMMIT saves all changes permanently. If any operation fails, ROLLBACK undoes all changes, ensuring the database remains consistent. This all-or-nothing approach is fundamental to reliable transaction processing.
Conditional COMMIT with Validation
123456789101112PERFORM VALIDATE-BUSINESS-RULES IF WS-VALIDATION-SUCCESS = 'Y' EXEC SQL COMMIT END-EXEC MOVE "SUCCESS" TO WS-RESULT-CODE ELSE EXEC SQL ROLLBACK END-EXEC MOVE "VALIDATION_FAILED" TO WS-RESULT-CODE END-IF
This pattern shows how to combine business rule validation with transaction control. After performing database operations, the program validates business rules to ensure the data meets all requirements. Only if validation passes does the program commit the changes. This approach prevents invalid data from being permanently saved and maintains business logic integrity alongside database consistency.
Tutorial: Implementing Robust Transaction Management
Step-by-Step Tutorial
Step 1: Design Transaction Boundaries
1234*> Define what constitutes a complete business transaction *> Example: Transfer money between accounts 01 WS-TRANSACTION-STATE PIC X VALUE 'N'. 01 WS-COMMIT-POINT PIC X VALUE 'N'.
Start by clearly defining what constitutes a complete business transaction. Identify all the database operations that must succeed together or fail together.
Step 2: Implement Error Checking
123456AFTER EACH SQL STATEMENT: IF SQLCODE NOT = 0 DISPLAY "Database error: " SQLCODE MOVE 'N' TO WS-COMMIT-POINT GO TO TRANSACTION-CLEANUP END-IF
Check SQLCODE after each database operation to detect errors immediately. Set flags to control whether the transaction should be committed or rolled back.
Step 3: Execute COMMIT or ROLLBACK
12345678TRANSACTION-CLEANUP. IF WS-COMMIT-POINT = 'Y' EXEC SQL COMMIT END-EXEC DISPLAY "Transaction committed successfully" ELSE EXEC SQL ROLLBACK END-EXEC DISPLAY "Transaction rolled back due to errors" END-IF
At the end of transaction processing, use your control flags to determine whether to commit or rollback the transaction based on the success of all operations.
Practical Exercises
Practice Exercises
Exercise 1: Order Processing System
Create a transaction that processes a customer order by updating inventory, creating an order record, and updating customer information. Use proper COMMIT/ROLLBACK logic.
Show Solution
12345678910111213141516171819PROCESS-ORDER. EXEC SQL UPDATE INVENTORY SET QUANTITY = QUANTITY - :WS-ORDER-QTY WHERE PRODUCT-ID = :WS-PRODUCT-ID END-EXEC IF SQLCODE = 0 EXEC SQL INSERT INTO ORDERS VALUES (:WS-ORDER-ID, :WS-CUSTOMER-ID, :WS-AMOUNT) END-EXEC END-IF IF SQLCODE = 0 EXEC SQL COMMIT END-EXEC ELSE EXEC SQL ROLLBACK END-EXEC END-IF.
Exercise 2: Banking Transaction
Implement a money transfer between two accounts with proper validation and transaction control. Ensure both debit and credit operations succeed or both fail.
Show Solution
12345678910111213141516171819202122TRANSFER-FUNDS. EXEC SQL UPDATE ACCOUNTS SET BALANCE = BALANCE - :WS-AMOUNT WHERE ACCOUNT-ID = :WS-FROM-ACCOUNT END-EXEC IF SQLCODE = 0 EXEC SQL UPDATE ACCOUNTS SET BALANCE = BALANCE + :WS-AMOUNT WHERE ACCOUNT-ID = :WS-TO-ACCOUNT END-EXEC END-IF IF SQLCODE = 0 EXEC SQL COMMIT END-EXEC DISPLAY "Transfer completed" ELSE EXEC SQL ROLLBACK END-EXEC DISPLAY "Transfer failed" END-IF.
Exercise 3: Batch Processing with Checkpoints
Design a batch processing program that processes records in groups and commits after every 100 successful updates to create checkpoints.
Show Solution
12345678910111213141516BATCH-PROCESS. PERFORM VARYING WS-COUNTER FROM 1 BY 1 UNTIL WS-COUNTER > WS-TOTAL-RECORDS PERFORM PROCESS-SINGLE-RECORD IF SQLCODE = 0 ADD 1 TO WS-SUCCESS-COUNT END-IF IF WS-SUCCESS-COUNT = 100 EXEC SQL COMMIT END-EXEC MOVE 0 TO WS-SUCCESS-COUNT DISPLAY "Checkpoint: 100 records committed" END-IF END-PERFORM.
Advanced COMMIT Strategies
Checkpoint Processing
In long-running batch processes, implement checkpoint processing by committing after processing a certain number of records. This prevents losing all work if the program fails partway through and allows for restart capability. Balance the checkpoint frequency between performance (fewer commits) and recovery time (more frequent commits).
Distributed Transaction Coordination
When working with multiple databases or systems, coordinate commits across all participants using two-phase commit protocols. This ensures that either all systems commit their changes or all roll back, maintaining consistency across the distributed environment.
Performance Considerations
COMMIT operations involve disk I/O and can impact performance. Group related operations into logical transactions and commit at appropriate intervals. Avoid committing after every single operation in high-volume scenarios, but don't make transactions so large that they consume excessive resources or increase lock contention.
Test Your Knowledge
Question 1: Transaction Control
What happens to database changes when COMMIT is executed?
Show Answer
B) Changes are permanently saved and visible to other programs - COMMIT makes all transaction changes permanent and releases locks.
Question 2: Error Handling
When should you use ROLLBACK instead of COMMIT?
Show Answer
B) When errors occur or validation fails - ROLLBACK undoes changes when the transaction cannot be completed successfully.
Question 3: Best Practices
What is a good strategy for long-running batch processes?
Show Answer
C) Use checkpoint processing with periodic commits - This balances performance with recovery capability and prevents losing all work on failure.