Udjuni.com

udjuni.com search:




Controlling SQL Transactions


Transactions in SQL are units of work that must be completed all together otherwise nothing is done. In other words, All required tasks must be complete otherwise whatever work is done must be rolled back. The famous and most common example of instances where SQL transactions are required is the banking. In this example if you were to transfer money from an account belonging to Mukoya to an account belonging to a client Munango, you would require the transaction to be complete when the money is subtracted from one account and added to another account. Under normal circumstances, that is what should happen, but what if something bad happens after the money is subtracted from Mukoya's account and before such an amount is added to the account of the recipient?

By default, the money will be lost and Mukoya will assume that he had successfully transferred the money, while Munango will be assuming that Mukoya has yet to transfer the money into his account. To avoid incidents where data is lost due to system interruptions, SQL implements what is called transaction handling. With transaction handling in place, if the transfer encounters trouble before it is finished, all the changes made up to that point are rolled back. In this case money subtracted from Mukoya's account that was not added to Munango's balance will be added back, leaving the balances in each account as they were before the transaction started.

Starting a Transaction

To start a transaction you simply use the following code:
BEGIN TRANSACTION; 
but if you have more than one transactions in the same block of code, you may want to consider giving your transactions names. You could do that by using the following syntax:
BEGIN TRANSACTION transaction_name; 


Here is my favourite example from a book I read when I was learning SQL:
begin transaction new_account
2> insert CUSTOMERS values ("Frans Mukoya", "1285 Mbambi village", "Rundu, Namibia", 6);
3> if exists(select * from CUSTOMERS where Name = "Frans Mukoya")
4> begin
5> begin transaction
6> insert BALANCES values(1650.76, 1461.26, 8);
7> end;
8> else
9> rollback transaction;
10> if exists(select * from BALANCES where Account_ID = 8);
11> begin
12> begin transaction
13> insert ACCOUNTS values(8, 6);
14> end;
15> else
16> rollback transaction;
17> if exists (select * from ACCOUNTS where Account_ID = 8 and Customer_ID = 6)
18> commit transaction; 
19> else
20> rollback transaction;
Notice in the example above that the code tests for the existence of a record, if that record does not exist, you roll back the transaction. Let us for example look at lines 2 up to 9. First we are adding a record to the CUSTOMERS table, but we are not sure if the record is successfully created, thus we test for its existence in line 3. If the record is found, we know it was successfully created and can now perform whatever functions we need to, which in this case is inserting the balances record for that customer. If the record was not found in line 3, we would abandon subsequent tasks and jump to line 8 where all work that had been performed so far are rolled back with line 9. Otherwise if all is well you will continue executing the code and then save your values using the Commit transaction statement.

Things to remember here:

  • Start transaction with BEGIN TRANSACTION
  • If something goes wrong roll back the changes made up to this point with ROLLBACK TRANSACTION
  • If everything runs smoothly to the end, complete the transaction with COMMIT TRANSACTION , which saves the data to permanent storage.

  • COMMITING Transactions with server settings


    One way to ensure that all transactions are committed / saved is to turn ON the server AUTOCOMMIT setting. That is done with the following statement:
    SET AUTOCOMMIT {ON| OFF}; 
    Note that you do not use the braces, you simply set AUTOCOMMIT to either ON or OFF as in
    SET AUTOCOMMIT ON; 
    to turn auto commit ON.

    Transaction Savepoints


    Save points serve as mini commits within a transaction. Say Now that you are in the transaction, but you have data that need to be saved if the transaction reaches that point so that if something goes wrong not all changes should be rolled back. This is where Save points comes into play. Take a look at the following example:
     1> SET TRANSACTION;
    2> UPDATE BALANCES SET CURR_BAL = 2600 WHERE ACCOUNT_ID = 6;
    3> SAVEPOINT save_point1;
    4> DELETE FROM BALANCES WHERE ACCOUNT_ID = 6;
    5> ROLLBACK TO SAVEPOINT save_point1;
    6> ROLLBACK;
    7> SELECT * FROM BALANCES;

    Note what happens on line 3, we are establish a save point after the update (Data is saved here so that the next roll back stops here and does not go all the way to the beginning of the transaction. Notice that after deleting the balance for account number 6, we decided that we just deleted from a wrong account. We use line 5 to roll back that deletion, but only up to the last update. Line 6 rolls back the whole transaction, thus the values in that record goes back to what it was before the transaction started.


RELATED ARTICLES:


Controlling cash flow problems
Cash flow refers to the way cash moves in and out of the business. In general, cash comes in when customers buy the products and services, and moves out of the business when you pay your debt to banks, suppliers and utility bills. As you can already see from this, a few things have an impact on your

Starting to Program in PHP
As defined in the posting “Introduction to programming”, is the art of giving step-by-step instructions to a computer telling it to do whatever you want. There are many programming languages ranging from what is termed first generation languages which are represented in strings of binary digits (zer

back it up reversibility


Starting to program in Java
Most java developers would describe it as an innovative programming language that enables a developer to write what is called "Applets" or pieces of java code that can be embedded in web pages. In addition to this benefit, compiled java code can be run on a variety of

Compiling and executing a java program
Java source code is always stored in a file with a .java file extension. Once you have created your source code, you simply save it with a .java extension and it is ready to be compiled. To compile your java code, you use the f

Did not find it? Try udjuni.com search:







© 2009 UdjunI LLC. All rights reserved | Privacy policy | Tutorial Index | RSS Feed