Transactions

TRANSACTION

A database transaction is a larger unit that frames multiple SQL statements. A transaction ensures that the action of the framed statements is atomic with respect to recovery.

A SQL Modification Statement has limited effect. A given statement can only directly modify the contents of a single table (Referential Integrity effects may cause indirect modification of other tables.) The upshot is that operations which require modification of several tables must involve multiple modification statements. A classic example is a bank operation that transfers funds from one type of account to another, requiring updates to 2 tables. Transactions provide a way to group these multiple statements in one atomic unit.

In SQL92, there is no BEGIN TRANSACTION statement. A transaction begins with the execution of a SQL-Data statement when there is no current transaction. All subsequent SQL-Data statements until COMMIT or ROLLBACK become part of the transaction. Execution of a COMMIT Statement or ROLLBACK Statement completes the current transaction. A subsequent SQL-Data statement starts a new transaction.

In terms of direct effect on the database, it is the SQL Modification Statements that are the main consideration since they change data. The total set of changes to the database by the modification statements in a transaction are treated as an atomic unit through the actions of the transaction. The set of changes either:

  • Is made fully persistent in the database through the action of the COMMIT Statement, or
  • Has no persistent effect whatever on the database, through:
    • the action of the ROLLBACK Statement,
    • abnormal termination of the client requesting the transaction, or
    • abnormal termination of the transaction by the DBMS. This may be an action by the system (deadlock resolution) or by an administrative agent, or it may be an abnormal termination of the DBMS itself. In the latter case, the DBMS must roll back any active transactions during recovery.

The DBMS must ensure that the effect of a transaction is not partial. All changes in a transaction must be made persistent, or no changes from the transaction must be made persistent.

Explicit Transaction

Commit

Saves Changes permanently to the Database in the Server

BEGIN TRANSACTION

COMMIT TRASACTION

Data Definition Language (DDL) are automatically committed. The importance of transactions in older versions of sql and oracle.

Rollback

This cancels the changes up to the previous commit or savepoint

BEGIN TRANSACTION

ROLLBACK TRANSACTION

Savepoint

This creates a marker in between transactions

BEGIN TRANSACTION

SAVE TRANSACTION S

[Note: Save point in Rollback Transaction

BEGIN TRANSACTION

ROLLBACK TRANSACTION S

S is the savepoint (restore point)]

Example

TABLE - EMPLOYEE

ENO

ENAME

SAL

Transactions

1

BIJU

45000

2

UNNI

42000

3

BALU

36000

Committed

4

GOPI

62000

5

HARI

22000

Savepoint S

6

UNNI

16000

7

GOVIND

39000

Rollback to S

Implicit Transaction

It means all the transactions are saved automatically.

Distributed Transaction

Distributed transactions span two or more Servers known as resource manager.