.Net - TransactionScope & Rollback

Why does the object implementing IEnlistmentNotification and forcing rollback in Prepare callback doesn't get notify to Rollback ? (my question may seem confusing, so here's a exemple)

Suppose class A implements IEnlistmentNotification and if "Value" is odd, then it forces rollback in the Prepare callback.

A a1 = null;
A a2 = null;

using (TransactionScope tx = new TransactionScope())
  a1 = new A();
  a2 = new A();

If we run the program, the result will be :

a1 will call Prepared(); // because 2 is even
a2 will call ForceRollback(); // because 3 is odd
a1 will call Rollback(); // because it gets notified to do so
the transaction fails with a ThreadAbordException
a1 has rollback
a2 hasn't rollback // lost of integretiy !!!!

Why doesn't a2 get notified to Rollback ? If a2 forced rollback, for sure it means he wants to get notified ! It's asking to rollback !

The problem is that in this situation, a1 has rollback but not a2, so there is a lost of integrity.

Can someone help me please figure out how it's suppose to work !?

Who is Participating?
See "Nuance #2"

"If you call ForceRollback() within your implementation of the Prepare() method, the Rollback() method will not be called on your resource.  "

Justification for this easily could have been that if an objects demands a rollback, than it already is aware of the rollback.
You know, I can assume that your example is a very simplified version of what you are actually trying to do, but I thought I should ask - is there a reason you don't simply handle this with nested transactions?  If class A represents a business object, I'm wondering why it also needs to be a resource manager.  It would be like SqlConnection having an "AddEmployee" method.  Just a thought,
jettaa2vr6Author Commented:
It's true, b_levitt, my example was a very simplified version of what I would like to do. To tell the truth, I am looking forward to implement SOA in our business : If SOA lies on WebServices, I was woundering how to handle transaction over multiple WebServices and for that reason I was looking at 2PC pattern. My research brought me to System.Transactions to encapsulate 2PC and I found stuff about IEnlistmentNotification and wanted to give it a try. So I created that simple A class to see how it would react. I totally agree that a business object shouldn't be a RM and for sure, a SqlConnection shouldn't be have a "AddEmployee" method ! I want to implement SOA, but I need to find a way, before I present the project to business managers, to make sure transactions will still work thru the system (even though it's WebServices's oriented).

By the way, thanks for the first info about the "ForceRollback". It was useful : now I understand why.
jettaa2vr6Author Commented:
Thank you for the excellent service and the speed at which the response was provided.
Transaction management from a service standpoint is really more about statefull sessions than it is the actual transaction.

In your case I would consider WCF which does support transactions (i beleive as an implementation of the ws-transaction standard):

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.