Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQLClient - Nested Transactions.  What are the possibilities?

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

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

722 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