views:

38

answers:

4

Hi,

Can you guys let me know the way of handling transactions in asp.net?

i.e. I have a few queries (Present in different functions and called under various situations) that have to be executed as a whole. So, how should I go about it?

Not sure of the syntax and the method/practice for writing the statements in .net (commit, rollback etc).

Kindly let me know. Also, plz point me to some good articles if possible. Thanks!!!

+1  A: 

I recommend using TransactionScope, because you can use it no mater what DB you are using. You can even do distributed transactions (operations against multiple databases within the same transaction) with it.

You can refer to a link for a code example, but in general, you do this:

try
{
    using (TransactionScope scope = new TransactionScope())
    {
        using (MySqlConnection connection1 = new MySqlConnection (connectionString))
        {
            // Opening the connection automatically enlists it in the 
            // TransactionScope as a lightweight transaction.
            connection1.Open();

            // create the DB commands and perform the DB operations
            .
            .
            .

            // The Complete method commits the transaction. If an exception has been thrown,
            // Complete is not called and the transaction is rolled back.
            scope.Complete();    
        }
    }
}
catch (Exception e)
{
    // something went wrong, handle the exception accordingly. Note
    // that since we did not call TransactionScope.Complete, nothing
    // gets committed to the DB.
}
dcp
In addition to that, you'll have to use InnoDB tables, as MyISAM tables doesn't support transactions.
nos
@nos - Good point.
dcp
Thanks dcp and nos. I have a few questions. The syntax seems to be similar to c#. Even for VB, the MSDN site suggests that we use Using. Any idea on why that is so? I read somewhere that in case of an exception, the scope.Complete would be invoked followed by a rollback. But, how would Complete be invoked? Won't the execution switch directly to the catch block? Thanks!
@rocksolid - If an exception occurs, then control will fall to the catch block and TransactionScope.Compete would NOT get called. When control falls out of the "using" block when we hit the catch, the TransactionScope would be disposed and the transaction itself would be rolled back. Remember, unless you call TransactionScope.Complete explicitly, the transaction is not comitted to the DB. And we put scope.Compete as the last statement at the end of the try block so we ensure alt he work is complete before comitting. Hope that helps.
dcp
Yes mate. Was helpful. Thanks a lot.
A: 

Here's another starter for TransactionScope: Implementing an Implicit Transaction using Transaction Scope

DOK
Thanks DOK.Is there a difference between using a TransactionScope object and MySqlTransaction object?
A: 

If its a local transaction you can also use ado.net's transaction object. TransactionScope will handle distributed transactions if needed but requires MSDTC to be configured if a transaction is promoted to a distributed transaction.

http://msdn.microsoft.com/en-us/library/2k2hy99x.aspx

Both are in the System.Transactions Namespace http://msdn.microsoft.com/en-us/library/system.transactions.aspx

Gratzy
May I know what a local transaction is? Is it something that accesses a single database and is not distributed over multiple databases/servers? If so, can I use a MySqlTransaction object? Thanks!
Yes that is what a local transaction is. I'm not familiar with MySqlTransaction object but if it's equivalent to an IDbTransaction then yes.
Gratzy
A: 

Don't know much about TransactionScope, but I just use the normal IDbTransaction like this:

IDbConnection conn = null;
IDbCommand cmd = null;
IDbTransaction tran = null;

try
{
    conn = DatabaseUtil.GetConnection(); //Get the connection somehow
    cmd = conn.CreateCommand();

    tran = conn.BeginTransaction();
    cmd.Transaction = tran;


    //Do your DB Work

    tran.Commit();
}
catch (SystemException ex)
{
    tran.Rollback();
}
finally
{
    if (conn != null) conn.Close();
}

With the IDb classes you are DB independent too to a certain degree.

Remy