Solved

SQLClient - Nested Transactions.  What are the possibilities?

Posted on 2004-08-26
1
689 Views
Last Modified: 2008-02-07
Good day all..

I have a need to be able to handel nested transactions.  It seems that what I want is not easily attained.
I just about have what I need but...

Here's the situation...
               
        I have a routine the reads a .sql file
        it is possible that there may be nested SQL within in this fine.
       
        BEGIN TRANSACTION
              SOME SQL
               BEGIN TRANSACTION
                      SOME SQL
               COMMIT
        COMMIT
       
        In my current test environment I am able to execute the inner transaction with out any errors.
        I then execute the outer transaction, which is broken...  
        I try to execute the outer transaction... it fails... so I try to roll it back.  
        This is where the problem occures.  I seems that when I commit the inner transaction that the
        that the outer transaction gets lost.    Not a good explanation but perhaps the code will do a better job.

        Dim con As New SqlClient.SqlConnection(sbConnectionString.ToString)
        If Not con.State = ConnectionState.Open Then
            con.Open()
        End If

        Dim cmd As SqlClient.SqlCommand = con.CreateCommand
        Dim trans As SqlTransaction
     
        trans = con.BeginTransaction
        cmd.Transaction = trans
     
        trans.save("Outer Trans")
        .CommandText = some SQL
        .ExecuteNonQuery()

        .CommandText = some SQL
        .ExecuteNonQuery()

        .CommandText = some BROKEN SQL
        .ExecuteNonQuery()

        .CommandText = some SQL
        .ExecuteNonQuery()

         trans.save("Inner Trans")

        .CommandText = some SQL
        .ExecuteNonQuery()

        .CommandText = some SQL
        .ExecuteNonQuery()

         trans.commit    ' Successfully commits

         trans.commit    '  This should fail...
   
        CATCH
           Trans.RollBack("Outer Trans")
                 ### THE PROBLEM OCCURES HERE###
                 An exception is thrown here saying the "Outer Trans"  Does not exist?
         
Perhaps I'm going about this all wrong...   Is there a better way to do this.  or do I tell the sender of these SQL files that we cannot except
nested transactions?


May thanks in advanced!


               
       
 
         
                         



0
Comment
Question by:haletjc
1 Comment
 
LVL 1

Accepted Solution

by:
MADeon earned 250 total points
Comment Utility
I believe that the transactions have to be given explicit names in order for nested transactions to work in SQL Server. Try:

        BEGIN TRANSACTION Trans1
              SOME SQL1
               BEGIN TRANSACTION Trans2
                      SOME SQL2
               COMMIT Trans2
        ROLLBACK Trans1

This should rollback both SQL1 and SQL2.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now