Results 1 to 7 of 7

Thread: SQLTransaction Question...

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2003
    Posts
    5

    Question SQLTransaction Question...

    I am writing an application that writes to two separate SQL Servers so I use two separate SQLConnection objects. Problem is, I would like to use a nested transaction so that if the parent transaction is rolled back then the inner transaction is also rolled back as below:

    -----------------------------------------------------------

    Declare Connection1
    Declare Connection2

    Open Connection1

    Open Connection2

    Begin Transaction For Connection1

    Update database for Connection1

    Begin Transaction For Connection2

    Update database for Connection2

    Commit Transaction For Connection2

    Rollback Transaction For Connection1 (I want this to rollback the data for Connection1 and Connection2)

    -----------------------------------------------------------

    Clearly the above does not work due to the transactions being associated with different connection objects. Is there any way this can be achieved in VB.NET?

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    How about this:

    VB Code:
    1. Dim trans As SqlTransaction = conn.BeginTransaction()
    2.  
    3.         Dim trans2 As SqlTransaction = conn2.BeginTransaction()
    4.  
    5. Try
    6.        'Your operations
    7.  
    8. Catch sqlx as SQLException
    9.  
    10. 'handle it
    11.        trans.Rollback()
    12.        trans2.Rollback()
    13.  
    14. Finally
    15.  
    16.        trans.Commit()
    17.        trans2.Commit()
    18.  
    19. End Try

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2003
    Posts
    5
    Well that would work, problem is, the two connections are in different DLLs (I probably should have mentioned that).

    Now I know I could "botch it" by exposing a method in the nested transaction DLL which could be used by the parent transaction DLL to call the commit/rollback remotely. I was just hoping that there was some other way.

  4. #4
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461
    Dear IAC77, I'm a lucky man, because I have to treat more simple problem than yours, but I'm interested on your question and on the ways we can solve it.....if possible!
    Mendhak give us a good suggestion but I'm trying to figure what happens if:

    trans.Commit()

    ---- System Crash, here according with Murphy's law ---------

    trans2.Commit()

    What happens to Trans2?
    Live long and prosper (Mr. Spock)

  5. #5

    Thread Starter
    New Member
    Join Date
    May 2003
    Posts
    5
    alextyx,

    That's a valid question, something I've never got round to finding out. I believe the data would be rolled-back (for trans2 only) by the server due to the connection being dropped when the application crashed. It seems very unlikely that the server would auto-commit the data.

  6. #6
    Frenzied Member maged's Avatar
    Join Date
    Nov 2002
    Location
    Egypt
    Posts
    1,040

    Re: SQLTransaction Question...

    i dont know, may be it's too late but

    Mendhak: i think your code is not right, why ? because the finally block is executed anyway whether there is an error or not. so in the case of exception the transaction will be rolledback then trying to execute the commit in the finally block (which will be executed anyway). the finally block itself will raise exception. so i think the commit should be moved from the finally to the last line of the try block. so that if it can't commit then rollback

    i know i am late , but i was searching for some information on the forums and i mbumbed out with this thread.

    one other thing. Mendhak is far more advanced than me, so i think it is just a typing mistake.

    rgds

  7. #7
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: SQLTransaction Question...

    Ah, that was from my early VB days

    Although this thread is more than a year old and completely irrelevant, I think I'd correct that post like this:

    VB Code:
    1. Dim trans As SqlTransaction = conn.BeginTransaction()
    2.  
    3.         Dim trans2 As SqlTransaction = conn2.BeginTransaction()
    4.  
    5. Try
    6.        'Your operations
    7.  
    8. trans.Commit()
    9.        trans2.Commit()
    10.  
    11. Catch sqlx as SQLException
    12.  
    13. 'handle it
    14.        trans.Rollback()
    15.        trans2.Rollback()
    16.  
    17. Finally
    18.  
    19.        
    20.  
    21. End Try

    This way any error will roll back both transactions. Of course, there may have to be checks in place to rollback the other transaction which might not even occur, so a boolean If-Then-Else can be placed there.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width