Is TransactionScopeConnections thread safe?

Topics: Data Access Application Block
Jun 18, 2010 at 3:42 PM
Edited Jun 18, 2010 at 3:56 PM
I have been studying the code for TransactionScopeConnections and I am having trouble understanding the intent of the threading logic. In the method GetConnection transactionConnections is first locked to retrieve a list of connections and after the lock is released the list of connections is then locked and a connection is either added or retrieved. If as suggested by the comments above the declaration of transactionConnections, it is possible that OnTransactionCompleted can be called on a different thread from the one the created the trnasaction then isn't the following scenario possible? 1. A call is made to GetTransaction and an existing connection list is returned from the first lock block. 2. Before the next lock block is entered, OnTransactionCompleted is called in a different thread and removes the collection obtained in step 1. 3. Coninuing in OnTransactionCompleted , a new connection is added to the collection which is no longer associated with a transaction so the connection won't be disposed creating a resource leak? I am I missing something here? It's not clear what scenario(s) the locking of the connection lists retreived from transactionConnections is protecting against.
Jun 21, 2010 at 5:24 AM

I don't think that's possible because after the transaction was completed on a different thread, the GetConnection will be returning a connection from a completed transaction and using that connection in your application will cause an exception.


Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.