TCL COMMIT


The COMMIT statement is used to save any changes made to the database during a transaction. When a COMMIT is issued, all changes within the transaction are permanently applied to the database.

The COMMIT statement is a transaction control statement used to finalize and apply changes, such as inserts, updates, or deletions, to the database. Once issued, these changes become permanent and visible to other users or applications accessing the database. The COMMIT statement is essential for:

  • Ensuring data integrity by applying all changes in a single transaction.
  • Making sure that modifications are not lost in case of program or system interruptions.

Without a COMMIT, changes remain in a temporary state and can be undone by issuing a ROLLBACK statement, which reverts the database to its state before the transaction began.

Syntax -

COMMIT;

The COMMIT statement is generally issued at the end of a set of related SQL commands, ensuring that all modifications are saved.

Using COMMIT Statements in a COBOL Program


In a COBOL-DB2 program, the COMMIT statement is embedded within an EXEC SQL block. After performing a series of data manipulation tasks (like INSERT, UPDATE, or DELETE), issuing a COMMIT will save the changes, ensuring they become permanent and visible to other processes.

General Steps for Using COMMIT in a COBOL Program:

  1. EXEC SQL: Start with the EXEC SQL block.
  2. COMMIT: Use the COMMIT statement after data modification statements.
  3. Error Handling: Check SQLCODE to ensure the COMMIT was successful.

Examples - Steps to Code an COMMIT Statement in a COBOL Program

* Step 1: Update Data in the Employee Table
	   EXEC SQL
		   UPDATE EMPLOYEE
		   SET SALARY = SALARY + 1000
		   WHERE JOB = 'MANAGER'
	   END-EXEC.
	   ...

   * Step 2: Commit the Transaction to Save Changes
	   EXEC SQL
		   COMMIT
	   END-EXEC.
	   ...
	   STOP RUN.

Error Handling for the COMMIT Statement in COBOL Program


Error handling in DB2 COBOL programs typically involves checking the SQLCODE after each SQL statement to confirm successful execution or identify errors:

  • SQLCODE = 0: The COMMIT was successful.
  • SQLCODE < 0: Indicates an error occurred, meaning the transaction was not saved; further action may be required, such as retrying the transaction or halting the program.

Examples - Error Handling in a COBOL Program

   EXEC SQL
	   COMMIT
   END-EXEC.

   IF SQLCODE = 0
	   DISPLAY 'Transaction committed successfully.'
   ELSE
	   DISPLAY 'Error in COMMIT operation: ' SQLCODE
   END-IF 
   STOP RUN.