HighTechTalks DotNet Forums  

Transactions and Try/Catch

Dotnet Framework (ADO.net) microsoft.public.dotnet.framework.adonet


Discuss Transactions and Try/Catch in the Dotnet Framework (ADO.net) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Jonathan Wood
 
Posts: n/a

Default Transactions and Try/Catch - 05-05-2009 , 02:19 PM






I've been unable to find any information on this:

If I create a SqlTransaction object with a using block, is calling Rollback
completely necessary if an exception occurs? It's hard to imagine that an
error that causes the using block to exit before encountering a Commit would
result in anything other than a rollback.

I know Rollback is preferred, but calling it from catch is a bit dicey. For
one thing, if I create the transaction object in a using block, then it
won't be available in a catch statement outside of that block. And for
another thing, I'd need another try/catch to handle any errors if the
Rollback fails.

Thanks for any tips.

Jonathan


Reply With Quote
  #2  
Old   
Miha Markic
 
Posts: n/a

Default Re: Transactions and Try/Catch - 05-06-2009 , 01:41 AM






Hi Jonathan,

No, you don't need to.
Rollback is called internally within Dispose method if Commit wasn't
called - that's the whole point of using "using": it makes sure you either
commit or rollback in timely fashion.
You can call Rollback earlier if you wish to.

--
Miha Markic [MVP C#, INETA Country Leader for Slovenia]
RightHand .NET consulting & development www.rthand.com
Blog: blog.rthand.com

"Jonathan Wood" <jwood (AT) softcircuits (DOT) com> wrote

Quote:
I've been unable to find any information on this:

If I create a SqlTransaction object with a using block, is calling
Rollback completely necessary if an exception occurs? It's hard to imagine
that an error that causes the using block to exit before encountering a
Commit would result in anything other than a rollback.

I know Rollback is preferred, but calling it from catch is a bit dicey.
For one thing, if I create the transaction object in a using block, then
it won't be available in a catch statement outside of that block. And for
another thing, I'd need another try/catch to handle any errors if the
Rollback fails.

Thanks for any tips.

Jonathan


Reply With Quote
  #3  
Old   
Jonathan Wood
 
Posts: n/a

Default Re: Transactions and Try/Catch - 05-06-2009 , 10:03 AM



Thanks Miha. That makes perfect sense to me. I guess this is documented
somewhere but I had trouble finding it.

Jonathan

"Miha Markic" <miha at rthand com> wrote

Quote:
Hi Jonathan,

No, you don't need to.
Rollback is called internally within Dispose method if Commit wasn't
called - that's the whole point of using "using": it makes sure you either
commit or rollback in timely fashion.
You can call Rollback earlier if you wish to.

--
Miha Markic [MVP C#, INETA Country Leader for Slovenia]
RightHand .NET consulting & development www.rthand.com
Blog: blog.rthand.com

"Jonathan Wood" <jwood (AT) softcircuits (DOT) com> wrote in message
news:#C8ouZbzJHA.3872 (AT) TK2MSFTNGP05 (DOT) phx.gbl...
I've been unable to find any information on this:

If I create a SqlTransaction object with a using block, is calling
Rollback completely necessary if an exception occurs? It's hard to
imagine that an error that causes the using block to exit before
encountering a Commit would result in anything other than a rollback.

I know Rollback is preferred, but calling it from catch is a bit dicey.
For one thing, if I create the transaction object in a using block, then
it won't be available in a catch statement outside of that block. And for
another thing, I'd need another try/catch to handle any errors if the
Rollback fails.

Thanks for any tips.

Jonathan



Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.