Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to rollback a .net transaction?

This question is related to my question: SQL Server and TransactionScope (with MSDTC): Sporadically can't get connection

I'm doing some transaction programming using the .net TransactionScope class. If I understand correctly, I can do some SQL operations within a transaction by wrapping the SQL calls inside a using ts as new TransactionScope() block, or by using new TransactionScope() and then TransactionScope.Dispose() at the end.

To commit the transaction, MSDN says to use TransactionScope.Commit(). Supposing that I want to rollback the transaction under certain circumstances, is it sufficient to simply call TransactionScope.Dispose() without calling the Commit method first? Is that good practice, or is this supposed to be done some other way?

like image 541
Vivian River Avatar asked Jun 21 '10 14:06

Vivian River


People also ask

Can we rollback transaction after COMMIT in C#?

You cannot roll back a transaction once it has been committed, because all modifications have become a permanent part of the database.


4 Answers

if you know you want to rollback then do that explicitly. You are not guaranteed that Dispose will rollback (in the case where complete has been called, the transaction will be committed when you call Dispose)

to your question about using or new/Dispose they are not equivalent

using(var ts = new TransactionScope())
{
}

is equivalent to

TransactionScope ts;
try
{
  ts = new TransactionScope();
}
finally
{
  ts.Dispose();
}

to answer your follow up question no if you call Dispose you will not have your transaction "hanging around" it will either commit or rollback. However if you use the new/dispose as you wrote it (no finally block) you can have a situation where dispose isn't called when you expect it to be (in the case of an exception)

like image 182
Rune FS Avatar answered Nov 15 '22 05:11

Rune FS


If you call TransactionScope.Dispose() (either by a using block or by calling the Dispose method yourself), it will rollback the transaction unless you tell it t commit first. By putting in a Transaction.Rollback, you are explicitly telling other programmers that is what you intend on doing.

I would probably add it for sake of clarity that this action was intended in that circumstance. The other thing about not explicitly adding the rollback command is that you are assuming that the Dispose method will always behave in this manner. In reality this will probably be the case, but making that assumption is risky. It's always better to explicitly roll it back instead of hoping that it will be done for you.

like image 41
kemiller2002 Avatar answered Nov 15 '22 04:11

kemiller2002


The intent of the TransactionScope class, as I see it, is to make transactions as foolproof as possible to the application developer, and to this end, make the recommended means of aborting a transaction be the one with the least extra thought. I believe (based on the documentation of TransactionScope) a simple Dispose is the recommended means of ending the transaction, and if it hasn't been committed, it will be rolled back.

like image 35
BlueMonkMN Avatar answered Nov 15 '22 04:11

BlueMonkMN


I've always encased the TransactionScope in a Using block, since that automatically calls the Dispose if there's an unhandled exception that prevents the transaction from completing successfully.

like image 38
SqlRyan Avatar answered Nov 15 '22 06:11

SqlRyan