Solved

SQLClient - Nested Transactions.  What are the possibilities?

Posted on 2004-08-26
1
694 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
ID: 11913396
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

947 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

22 Experts available now in Live!

Get 1:1 Help Now