Registering and unregistering an object to a transaction.

alanchinese

Regular
Joined
Jan 12, 2005
Messages
62
Hi,

In the "Remarks" section of System.Transactions.CommitableTransaction.Commit() method, it says
When this method is called, all objects that have registered to participate in the transaction are polled and can independently indicate their vote to either commit or roll back the transaction. If any participant votes to roll back the transaction, it is rolled back and this method throws a TransactionException exception.

Can anyone post some sample codes on how an object is registered to a transaction, and how this object itself get unregistered?

I am catching one of the ExecuteNonQuery failures and want to retry again. The current transaction's TransactionInformation.Status is "Active" until after the Commit() method call. I think it detects there was at least one failure but I don't know how to clean it up before the Commit() call.

Thanks a lot,
Alan.
 
Anyone has a clue? I can't figure out the unregistering part.

Here is the actual thing I want to do:

I have a transaction, trying to execute two insert queries.
It's not important if the first one is successful or not - I will catch the exception and move on, o/w commit to the db.
All I care is commiting the second one.
However, the Transaction.Commit() will roll back everything if the first one fails.
 
Could you post the code you have? Just out of interest is there a reason you are including the first insert in the transaction if the second one is all you care about? Or is it that if the second one fails you want to roll back both inserts?
 
To make it simple, the first insert is "optional". That means, it's not significant to commit the first insert even though it belongs to part of the business logic. When it fails, it's designed to be ignored and all the other queries need to be commited.

Another occassion is, when the network connection fails for a temporary 2 minutes, calling the ExecuteNonQuery() method will get a TCP-IP error. I want to be able to catch this specific error and wait for 3 minutes before I attemp a new connection and call ExecuteNonQuery() again. However, when the Transaction.Commit() method is called, the TCP-IP error is still counted as a failed enlistment. I cannot find a way to get rid of that bad apple. Everything would be rolled back because of that.
 
Back
Top