TransactionScope exception handling

D

Darren

Is there any way to have an DataReader.Read part of a TransactionScope but not cancel the transaction if the read fails?

I'm using a DataReader to get the result of a stored procedure which increments a counter. I'd like to add the read to
the transaction only if the read is successful. If the read isn't successful I don't want it to cancel the entire
distributed transaction but I will need to roll it back (since the SP updates a table) if some other part of the transaction fails.
 
T

Tor Bådshaug

My first thought was that you may take advantage of
TransactionScopeOption.Suppress, but reading your case a second time I see
that will not help as the "read" operation actually does some writing.

My suggestion is that your invocation of the DataReader.Read is surrounded
by a try-catch clause. This way you can continue your transactional work and
eventually setting the transaction's consistency bit (using scope.Complete)
even if your DataReader.Read invocation fails. If your other
transactional work fails, the DataReader.Read invocation would be part of
the same transaction and will be rolled back as you want.

Hope it helps.

Tor Bådshaug
tor.badshaug(AT)bekk.no
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top