Solved

SQLClient - Nested Transactions.  What are the possibilities?

Posted on 2004-08-26
1
718 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

617 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