Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQLClient - Nested Transactions.  What are the possibilities?

Posted on 2004-08-26
1
Medium Priority
?
730 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 750 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
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…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

971 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