In general I would not recommend mixing DbTransactions with System.Transactions transactions, because you can run into precisely these types of issues where something in the inner transaction affects the overall behavior of the outer transaction. Having said that, I'll try to provide some information on what's going on, and your options.
Which backend are you targetting here My response is based on how SQL Server handles transactions, but depending on what you are using, the behavior may be different.
I'm not sure if you understand why the exception is thrown, but just want to know how to handle it, or if you are questioning why it's happening in the first place. In the given example, with SQL Server, if you do a ROLLBACK in the middle of a nested transaction, it will rollback the entire transaction, which is why the error occurs when the Complete method tries to Commit it.
I realize this is probably a simplified example, but I don't know how common this scenario is as you have described it. Again with SQL Server, if you Commit in the middle of a nested transaction, it doesn't really do much, because the outer transaction is still active. In this case, when you do the rollback, it's rolling back everything, including the changes you make in the first BeginTransaction/Commit block. However, even if you remove the BeginTransaction/Commit block, you will still see the TransactionAbortedException. I'm just mentioning it because I don't fully understand the intent of the scenario, and why you are trying to use nested transactions at all (or, why they are both wrapped in an outer TransactionScope).
If you don't call Complete, you shouldn't get the exception. However, System.Transactions will still try to rollback the transaction. It will either attempt to commit if you use Complete, or attempt to Rollback. You can't really get around it trying one or the other. In this case, SQL Server will still throw an error even if you don't call Complete (you can see it in SQL Profiler), because the transaction is not longer active. It's just that the exception is not propagated to the client code.
If for some reason you can't avoid the Complete call, you will have to handle the TransactionAbortedException.
Thanks, Sarah
Please Mark as Answer if this answers your question, or Unmark as Answer if it is marked and you feel it is not answered.
More info on Nested Transactions and SQL Server: Nesting Transactions http://msdn2.microsoft.com/en-us/library/ms189336.aspx
|