transaction notes

Upload: anup-mishra

Post on 16-Feb-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/23/2019 Transaction Notes

    1/4

    A transaction is a unit of work that is performed against a database. Transactions are units or sequences of work accomplished in a logical order, whether in a manual fashion by a user or

    automatically by some sort of a database program.

    A transaction is the propagation of one or more changes to the database. For example, if you are creating a record or updating a record or deleting a record from the table, then you are

    performing transaction on the table. It is important to control transactions to ensure data integrity and to handle database errors.

    Practically, you will club many SQL queries into a group and you will execute all of them together as a part of a transaction.

    Properties of Transactions:Transactions have the following four standard properties, usually referred to by the acronym ACID:

    Atomicity:ensures that all operations within the work unit are completed successfully; otherwise, the transaction is aborted at the point of failure, and previous operations are

    rolled back to their former state.

    Consistency:ensures that the database properly changes states upon a successfully committed transaction.

    Isolation:enables transactions to operate independently of and transparent to each other.

    Durability:ensures that the result or effect of a committed transaction persists in case of a system failure.

    Transaction Control:There are following commands used to control transactions:

    COMMIT:to save the changes.

    ROLLBACK:to rollback the changes.

    SAVEPOINT:creates points within groups of transactions in which to ROLLBACK

    SET TRANSACTION:Places a name on a transaction.

    Transactional control commands are only used with the DML commands INSERT, UPDATE and DELETE only. They can not be used while creating tables or dropping them because these

    operations are automatically commited in the database.

    The COMMIT Command:The COMMIT command is the transactional command used to save changes invoked by a transaction to the database.

    The COMMIT command saves all transactions to the database since the last COMMIT or ROLLBACK command.

    The syntax for COMMIT command is as follows:

    COMMIT;

    Example:Consider the CUSTOMERS table having the following records:

    +----+----------+-----+-----------+----------+|ID |NAME |AGE |ADDRESS |SALARY |

    +----+----------+-----+-----------+----------+| 1|Ramesh | 32|Ahmedabad| 2000.00|

    | 2|h!"a# | 2$|De"h! | 1$00.00|| 3|%a&sh!% | 23|'(a | 2000.00|| )|Cha!(a"!| 2$|M&mba! | *$00.00|| $|a,d!% | 2|h'/a" | $00.00|| *|'ma" | 22|M | )$00.00|

    | |M& | 2)|I#d',e |10000.00|+----+----------+-----+-----------+----------+

    Following is the example which would delete records from the table having age = 25 and then COMMIT the changes in the database.

    S4L5DELETE 6ROM C7STOMERS

    8ERE AGE 92$;S4L5COMMIT;

    As a result, two rows from the table would be deleted and SELECT statement would produce the following result:

    +----+----------+-----+-----------+----------+

  • 7/23/2019 Transaction Notes

    2/4

    |ID |NAME |AGE |ADDRESS |SALARY |+----+----------+-----+-----------+----------+| 1|Ramesh | 32|Ahmedabad| 2000.00|| 3|%a&sh!% | 23|'(a | 2000.00|| $|a,d!% | 2|h'/a" | $00.00|| *|'ma" | 22|M | )$00.00|| |M& | 2)|I#d',e |10000.00|

    +----+----------+-----+-----------+----------+

    The ROLLBACK Command:The ROLLBACK command is the transactional command used to undo transactions that have not already been saved to the database.

    The ROLLBACK command can only be used to undo transactions since the last COMMIT or ROLLBACK command was issued.

    The syntax for ROLLBACK command is as follows:

    ROLLAC;

    Example:Consider the CUSTOMERS table having the following records:

    +----+----------+-----+-----------+----------+|ID |NAME |AGE |ADDRESS |SALARY |

    +----+----------+-----+-----------+----------+| 1|Ramesh | 32|Ahmedabad| 2000.00|

    | 2|h!"a# | 2$|De"h! | 1$00.00|| 3|%a&sh!% | 23|'(a | 2000.00|| )|Cha!(a"!| 2$|M&mba! | *$00.00|| $|a,d!% | 2|h'/a" | $00.00|| *|'ma" | 22|M | )$00.00|| |M& | 2)|I#d',e |10000.00|+----+----------+-----+-----------+----------+

    Following is the example, which would delete records from the table having age = 25 and then ROLLBACK the changes in the database.

    S4L5DELETE 6ROM C7STOMERS

    8ERE AGE 92$;S4L5ROLLAC;

    As a result, delete operation would not impact the table and SELECT statement would produce the following result:

    +----+----------+-----+-----------+----------+|ID |NAME |AGE |ADDRESS |SALARY |+----+----------+-----+-----------+----------+| 1|Ramesh | 32|Ahmedabad| 2000.00|| 2|h!"a# | 2$|De"h! | 1$00.00|| 3|%a&sh!% | 23|'(a | 2000.00|| )|Cha!(a"!| 2$|M&mba! | *$00.00|| $|a,d!% | 2|h'/a" | $00.00|| *|'ma" | 22|M | )$00.00|| |M& | 2)|I#d',e |10000.00|

    +----+----------+-----+-----------+----------+

    The SAVEPOINT Command:A SAVEPOINT is a point in a transaction when you can roll the transaction back to a certain point without rolling back the entire transaction.

    The syntax for SAVEPOINT command is as follows:

    SA:EOINT SA:EOINTNAME;

    This command serves only in the creation of a SAVEPOINT among transactional statements. The ROLLBACK command is used to undo a group of transactions.

    The syntax for rolling back to a SAVEPOINT is as follows:

    ROLLAC TO SA:EOINTNAME;

    Following is an example where you plan to delete the three different records from the CUSTOMERS table. You want to create a SAVEPOINT before each delete, so that you can ROLLBACK to

    any SAVEPOINT at any time to return the appropriate data to its original state:

  • 7/23/2019 Transaction Notes

    3/4

    Example:Consider the CUSTOMERS table having the following records:

    +----+----------+-----+-----------+----------+|ID |NAME |AGE |ADDRESS |SALARY |+----+----------+-----+-----------+----------+

    | 1|Ramesh | 32|Ahmedabad| 2000.00|| 2|h!"a# | 2$|De"h! | 1$00.00|| 3|%a&sh!% | 23|'(a | 2000.00|| )|Cha!(a"!| 2$|M&mba! | *$00.00|| $|a,d!% | 2|h'/a" | $00.00|| *|'ma" | 22|M | )$00.00|| |M& | 2)|I#d',e |10000.00|+----+----------+-----+-----------+----------+

    Now, here is the series of operations:

    S4L5SA:EOINT S1;Sa de"e(ed.S4L5SA:EOINT S2;Sa de"e(ed.S4L5SA:EOINT S3;Sa de"e(ed.

    Now that the three deletions have taken place, say you have changed your mind and decided to ROLLBACK to the SAVEPOINT that you identified as SP2. Because SP2 was created after the

    first deletion, the last two deletions are undone:

    S4L5ROLLAC TO S2;R'""ba=%='m/"e(e.

    Notice that only the first deletion took place since you rolled back to SP2:

    S4L5SELECT ?6ROM C7STOMERS;+----+----------+-----+-----------+----------+|ID |NAME |AGE |ADDRESS |SALARY |+----+----------+-----+-----------+----------+

    | 2|h!"a# | 2$|De"h! | 1$00.00|| 3|%a&sh!% | 23|'(a | 2000.00|

    | )|Cha!(a"!| 2$|M&mba! | *$00.00|| $|a,d!% | 2|h'/a" | $00.00|

    | *|'ma" | 22|M | )$00.00|

    | |M& | 2)|I#d',e |10000.00|+----+----------+-----+-----------+----------+*,'>s se"e=(ed.

    The RELEASE SAVEPOINT Command:The RELEASE SAVEPOINT command is used to remove a SAVEPOINT that you have created.

    The syntax for RELEASE SAVEPOINT is as follows:

    RELEASE SA:EOINT SA:EOINTNAME;

    Once a SAVEPOINT has been released, you can no longer use the ROLLBACK command to undo transactions performed since the SAVEPOINT.

    The SET TRANSACTION Command:The SET TRANSACTION command can be used to initiate a database transaction. This command is used to specify characteristics for the transaction that follows.

    For example, you can specify a transaction to be read only, or read write.

    The syntax for SET TRANSACTION is as follows:

    SET TRANSACTION @READ 8RITE |READ ONLY ;

  • 7/23/2019 Transaction Notes

    4/4

    Normally, it's best to allow SQL Server to enforce isolation

    between transactionsin its default manner; after all, isolation is one of the basic

    tenets of theACID model !owever, sometimes business re"uirements force

    database administrators to stray from the default behavior and ado#t a less ri$id

    a##roach to transaction isolation %o assist in such cases, SQL Server offers fivedifferent transaction isolation models &efore tain$ a detailed loo at SQL

    Server's isolation models, we must first e(#lore several of the database

    concurrency issues that they combat)

    Dirty Readsoccur when one transaction reads data written by another, uncommitted, transaction %he dan$er with dirty reads is

    that the other transaction mi$ht never commit, leavin$ the ori$inal transaction with *dirty* data

    Non-repeatable Readsoccur when one transaction attem#ts to access the same data twice and a second transaction modifies

    the data between the first transaction's read attem#ts %his may cause the first transaction to read two different values for the same data,

    causin$ the ori$inal read to be non+re#eatable

    Phantom Readsoccur when one transaction accesses a ran$e of data more than once and a second transaction inserts or deletes

    rows that fall within that ran$e between the first transaction's read attem#ts %his can cause *#hantom* rows to a##ear or disa##ear from

    the first transaction's #ers#ective

    SQL Server's isolation models each attem#t to con"uer a subset of these

    #roblems, #rovidin$ database administrators with a way to balance transaction

    isolation and business re"uirements %he five SQL Server isolation models are)

    %he Read Committed Isolation Modelis SQL Servers default behavior In this model, the database does not allow

    transactions to read data written to a table by an uncommitted transaction %his model #rotects a$ainst dirty reads, but #rovides no

    #rotection a$ainst #hantom reads or non+re#eatable reads

    %he Read Uncommitted Isolation Modeloffers essentially no isolation between transactions Any transaction can read data

    written by an uncommitted transaction %his leaves the transactions vulnerable to dirty reads, #hantom reads and non+re#eatable reads

    %he Repeatable Read Isolation Model$oes a ste# further than the -ead Committed model by #reventin$ transactions from

    writin$ data that was read by another transaction until the readin$ transaction com#letes %his isolation model #rotect a$ainst both dirty

    reads and non+re#eatable reads

    %he Serializable Isolation Modeluses ran$e locs to #revent transactions from insertin$ or deletin$ rows in a ran$e bein$

    read by another transaction %he Seriali.able model #rotects a$ainst all three concurrency #roblems

    %he Snapshot Isolation Modelalso #rotects a$ainst all three concurrency #roblems, but does so in a different manner It

    #rovides each transaction with a *sna#shot* of the data i t re"uests %he transaction may then access that sna#shot for all future

    references, eliminatin$ the need to return to the source table for #otentially dirty dataIf you need to chan$e the isolation model in use by SQL Server, sim#ly issue the command)

    SET TRANSACTION ISOLATION LE:EL B"e