Re: Transaction.Commit() and Transaction.Rollback()



They are used to ensure that an entire group of commands succeeds or else none of them succeed. For example, suppose you are writing software to manage bank transfers. Image that there are two people's accounts Joe and Fred.

Joe Fred
$500 $400

Now Joe wants to transfer $300 to Fred. This transaction will consist of two queries. A subtraction of $300 from Joe's account and an addition of $300 to Fred's account. Now consider what happens if the power goes out, or the network gets cut or the server runs out of disk space, etc after the first of the two queries. Depending on whether you did the subtraction for Joe first or the addition to Fred first you can end up with the following two scenarios.

Joe Fred
$200 $400

Joe Fred
$500 $700

As you can see. If the $300 was taken from Joe's account first then $300 basically vanishes from the system. If the $300 is given to fred first then you've basically given away money.

To ensure the integrity of the system you need to be able to guarantee that either both queries succeed or neither of them does. This guarantee of all actions succeeding or failing as a group is referred to as Atomic. This is done through the use of a transaction log.

The Begin Transaction, Commit Transaction, End Transaction & Rollback Transaction commands are ways to specify which groups of queries to treat as Atomic.

Begin Transaction says that you are entering an atomic section of code.

Commit & End Transaction tells the database that your are finished with that atomic operation and that all the commands you had sent since the trasaction began should now get committed to the actual database tables.

Rollback transaction tells the database that you want to undo everything you did after you started the transaction.

To use the example above if you did this set of operations

Begin Trasnaction
Withdraw $300 from Joe
Deposit $300 to Fred
Commit Transaction

You have a guarantee from the database that the total balance between Fred & Joe will be $900 even if there is a power loss. No losses or additions as with the previous case.

Hopefully my description has made it clear the difference between the transaction commands and the query commands like ExecuteReader.

If you'd like to know more, I recommend you read up on ACID (Atomicity, Consistency, Isolation, Durability) properties of a database.

--
Andrew Faust
andrew[at]andrewfaust.com
http://www.andrewfaust.com


"weird0" <amirediwan@xxxxxxxxx> wrote in message news:1194071644.179259.127160@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Transaction.Commit() , Transaction.Rollback() ,
cmd.BeginTransaction(), cmd.EndTransaction()

Can any one explain what are the kind of scenarios that these set of
commands are being used. MSDN does have the code but does not explain
how are they different from simple ExecuteReader() or executing any
simple query.

Referring to any useful link, would be appreciated.

Regards


.



Relevant Pages

  • Re: Transaction Identifier in transaction replication
    ... Sql server shows the value of the transaction id when we execute following two commands: ... I need to read this value in custom stored procedure of subscribed database. ... In your case you can't modify the source app, ...
    (microsoft.public.sqlserver.replication)
  • Re: Multiple concurrent isolated transactions on ADO Dataset
    ... and an allocation is too great to warrant one short transaction, also, there ... > transaction and view should be updated after it. ... The commands are stacked up ... >> database transaction which succeeds or fails. ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: ExecuteNonQuery stops program
    ... > Are these commands a part of a transaction? ... > - Sahil Malik ... No, separate inserts into database. ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: transaction and open table
    ... some cmds only, and other cmds I do not use trasaction, esp those read ... If the transaction is properly isolated, then those commands that you execute outside of the transaction will not see the changes that you have done inside the transaction until the transaction is committed. ... Moreover, since the transaction will place locks on the database to accomplish the isolation, depending on what you are doing you risk that the database commands that you are executing outside of the transaction might get blocked waiting for the transaction to finish. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: dbf trashing problem after upgrade
    ... FLUSH doesn't work on disks that have an active Write cache, ... Tha'ts why VFP in recent versions has added commands like SYS'Purge ... data modifications or as the BEGIN TRANSACTION - END TRANSACTION / ROLLBACK ... the terminals share a DBF on the server. ...
    (microsoft.public.fox.programmer.exchange)