Skip to content Skip to sidebar Skip to footer

Understanding Transactionscope Timeouts

My current understanding of transactionscope timeouts. If a transaction has been running longer than the set timeout time it throws an exception when transaction.complete() is ca

Solution 1:

Try to look at it this way:

The length of the transaction is only determined when you call trans.Complete() or exit the transaction scope. Take the following code:

using (var trans= new TransactionScope())
{
Threading.Sleep(99999);
trans.Complete()
}

There is no way to throw a timeout exception while inside the sleep routine and it wouldn't make sense if it did. And so using transaction timeouts (this way at least) can only guarantee that if the transaction takes longer than your timeout, it will not be commited.

If you are just executing one query (which I wouln't know what you use the transactions for) then you could set the query/command timeout (or whatever you call it). IIRC, your query will return immediately after the timeout expires.

Another way would be to set your web service request timeout and just assume that the webservice is taking too long to respond because of whatever was inside your transaction.

EDIT: You could try:

  • Spawning your transaction on a different thread and then wait for it to complete (using Thread.Join(timeout)) on your main thread (one used by the webservice call). So if it doesn't terminate before the timeout you specified then you could stop waiting and return a timeout error (dont forget to signal the other thread to abort the transaction).
  • Assuming you are only performing SQL queries inside those transactions, you could use the "BEGIN TRANSACTION" keyword to specify the transaction in the sql script (hacky indeed). Then you could just specify the command timeout and execute all this in a single line of code. But then this requires you to move everything you do inside the transaction into a sql script which may or may not be possible for you... and it isn't clean.

Post a Comment for "Understanding Transactionscope Timeouts"