6 Columns with Active Background
6 Columns with Active Background
Active List Item Background

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.

WRITETransaction 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.

    WRITETransaction 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.

  WRITETransaction 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.

WRITETransaction 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.

    WRITETransaction 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.

  WRITETransaction 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