- SAP ABAP
- What is SAP ABAP
- SAP ABAP Data Dictionary and Domain
- SAP ABAP Data Element
- SAP ABAP Database Table
- SAP ABAP Database tables and views
- SAP ABAP Foreign Key
- SAP ABAP Indexes
- SAP ABAP Structure
- SAP ABAP Package
- SAP ABAP Adding Fields to SAP Standard Table
- SAP ABAP Internal Table and Database Table
- SAP ABAP Select Option and Parameter
- SAP ABAP Types of Internal Table
- SAP ABAP ways of Declaring Internal Tables
- SAP ABAP Mastering Initialization Technique
- SAP ABAP Operations on Internal Table
- SAP ABAP Record Retrieval
- SAP ABAP Insert, Modify and Delete data in the Internal table by using Keywords
- SAP ABAP Sorting and Removing Adjacent Duplicates
- SAP ABAP Seamless Data Transfer Between Internal Tables
- SAP ABAP Search Help Types
- SAP ABAP Lock Objects and Types
- SAP ABAP Buffering and Its Types
- SAP ABAP TMG
- SAP ABAP Table Types
- SAP ABAP Views
- SAP ABAP Control Break Statement
- SAP ABAP COMMIT and ROLLBACK
- SAP ABAP Joins
- SAP For All Entries
- SAP ABAP Procedure to Fill Final Internal Table
- SAP ABAP Modularization
- SAP ABAP Function Group and Function Module
- SAP ABAP SELECT Options
24DDIC0508 – Understanding COMMIT WORK and ROLLBACK WORK
What is a Database Transaction?
A database transaction is a sequence of operations performed as a single logical unit of work. Transactions ensure that either all operations are completed successfully, or none are, maintaining the consistency and integrity of the database. In ABAP, transactions typically involve multiple database operations, such as inserts, updates, and deletes.
COMMIT WORK
The COMMIT WORK statement in ABAP is used to save all changes made during the current transaction to the database permanently. When a COMMIT WORK is issued, the database writes all the changes made since the start of the transaction to disk, making them permanent and visible to other transactions.
Key Features:
Finalizes and saves all changes made during the transaction.
Releases any database locks held by the transaction.
Triggers COMMIT processing logic, such as update function modules or event handling.
Syntax:
COMMIT WORK.
Example:
START OF SELECTION.
” Insert a new customer
INSERT INTO zcustomers VALUES (‘C001’, ‘John Doe’, ‘New York’).
” Insert a new order for the customer
INSERT INTO zorders VALUES (‘O001’, ‘C001’, ‘Laptop’, 2).
” Commit the transaction
COMMIT WORK.
WRITE ‘Transaction committed successfully.‘.
In this example, two insert operations are performed to add a new customer and a new order. The COMMIT WORK statement ensures that both operations are saved to the database permanently.
ROLLBACK WORK
The ROLLBACK WORK statement in ABAP is used to undo all changes made during the current transaction. When a ROLLBACK WORK is issued, the database discards all changes made since the start of the transaction, restoring the database to its previous state.
Key Features:
Discards all changes made during the transaction.
Releases any database locks held by the transaction.
Ensures data consistency by preventing partial updates.
Syntax:
ROLLBACK WORK.
Example:
START OF SELECTION.
” Insert a new customer
INSERT INTO zcustomers VALUES (‘C002’, ‘Jane Smith’, ‘Los Angeles’).
” Simulate an error condition
IF sy-subrc <> 0.
” Rollback the transaction
ROLLBACK WORK.
WRITE ‘Transaction rolled back due to an error.‘.
EXIT.
ENDIF.
” Insert a new order for the customer
INSERT INTO zorders VALUES (‘O002’, ‘C002’, ‘Smartphone’, 1).
” Commit the transaction
COMMIT WORK.
WRITE ‘Transaction committed successfully.‘.
In this example, if an error occurs during the first insert operation, the ROLLBACK WORK statement is issued, discarding any changes made and preventing partial updates to the database.
When to Use COMMIT WORK and ROLLBACK WORK
COMMIT WORK should be used when:
All database operations within a transaction are completed successfully.
You want to make the changes permanent and visible to other transactions.
ROLLBACK WORK should be used when:
An error occurs during the transaction, and you want to discard all changes.
You want to ensure data consistency by preventing partial updates.
Author : Aniket Pawar, 9373518385
24DDIC0508 – Understanding COMMIT WORK and ROLLBACK WORK
What is a Database Transaction?
A database transaction is a sequence of operations performed as a single logical unit of work. Transactions ensure that either all operations are completed successfully, or none are, maintaining the consistency and integrity of the database. In ABAP, transactions typically involve multiple database operations, such as inserts, updates, and deletes.
COMMIT WORK
The COMMIT WORK statement in ABAP is used to save all changes made during the current transaction to the database permanently. When a COMMIT WORK is issued, the database writes all the changes made since the start of the transaction to disk, making them permanent and visible to other transactions.
Key Features:
Finalizes and saves all changes made during the transaction.
Releases any database locks held by the transaction.
Triggers COMMIT processing logic, such as update function modules or event handling.
Syntax:
COMMIT WORK.
Example:
START OF SELECTION.
” Insert a new customer
INSERT INTO zcustomers VALUES (‘C001’, ‘John Doe’, ‘New York’).
” Insert a new order for the customer
INSERT INTO zorders VALUES (‘O001’, ‘C001’, ‘Laptop’, 2).
” Commit the transaction
COMMIT WORK.
WRITE ‘Transaction committed successfully.‘.
In this example, two insert operations are performed to add a new customer and a new order. The COMMIT WORK statement ensures that both operations are saved to the database permanently.
ROLLBACK WORK
The ROLLBACK WORK statement in ABAP is used to undo all changes made during the current transaction. When a ROLLBACK WORK is issued, the database discards all changes made since the start of the transaction, restoring the database to its previous state.
Key Features:
Discards all changes made during the transaction.
Releases any database locks held by the transaction.
Ensures data consistency by preventing partial updates.
Syntax:
ROLLBACK WORK.
Example:
START OF SELECTION.
” Insert a new customer
INSERT INTO zcustomers VALUES (‘C002’, ‘Jane Smith’, ‘Los Angeles’).
” Simulate an error condition
IF sy-subrc <> 0.
” Rollback the transaction
ROLLBACK WORK.
WRITE ‘Transaction rolled back due to an error.‘.
EXIT.
ENDIF.
” Insert a new order for the customer
INSERT INTO zorders VALUES (‘O002’, ‘C002’, ‘Smartphone’, 1).
” Commit the transaction
COMMIT WORK.
WRITE ‘Transaction committed successfully.‘.
In this example, if an error occurs during the first insert operation, the ROLLBACK WORK statement is issued, discarding any changes made and preventing partial updates to the database.
When to Use COMMIT WORK and ROLLBACK WORK
COMMIT WORK should be used when:
All database operations within a transaction are completed successfully.
You want to make the changes permanent and visible to other transactions.
ROLLBACK WORK should be used when:
An error occurs during the transaction, and you want to discard all changes.
You want to ensure data consistency by preventing partial updates.
Author : Aniket Pawar, 9373518385