Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / programming / exceptions

Details on Transactionscope exception handling

0.00/5 (No votes)
30 May 2011CPOL1 min read 18.8K  
How to do transaction scope exception handling.
I think the documentation on TransactionAbortedException is a little lacking as to when it can happen.

It does not happen if you do not do a Scope.Complete on your initial transaction. In other words, failure to execute Scope.Complete means that it will silently exit a transaction scope using block. (Since there is no other way to cancel the transaction scope silently, this is acceptable but should be documented clearly.)
If you need to alert your processes that you are aborting the initial transaction, you should do so by throwing an exception, which will silently close the transaction scope automatically. You may want to have a try/catch wrapping your transaction scope using block to catch these exceptions.

However, if you have multiple same level calls to processes that have their own transaction scope using blocks that join the ambient transaction, you will get an exception if you fail to Scope.Complete on a call. When a subsequent call initiates the transaction scope, it sees a dirty transaction and will issue the transactionabortedexception at that point. However, if you are nesting calls and the child call does not issue a scope.complete, then any other child call open above it will not issue an abort exception when it issues a scope.complete(), it will only be caught if another transaction is attempted or when the initial transasction scope issues a scope.complete.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)