System.Transactions accross threads

G

Guest

I have a problem using System.Transactions accross threads and would welcome
any help.

My basic scenario is as follows:

Main Thread

-Create TransactionScope on main thread
-Open connection to SQL Server 2005 database
-Create Thread
-Start thread passing DependentTransaction
-Call TransactionScope.Complete

Worker Thread

-Create TransactionScope (using DependentTransaction)
-Open connection to SAME Database
-Call TransactionScope.Complete
-Call DependentTransaction.Complete

The code for this is below:

public void ThreadedSystemTransactions()
{
using (SqlConnection c1 = new SqlConnection(@"Data Source=(local);Initial
Catalog=TXNTEST2;Integrated Security=True"))
{
using (TransactionScope ts = new TransactionScope())
{
c1.Open();

DependentTransaction dtx =
Transaction.Current.DependentClone(DependentCloneOption.BlockCommitUntilComplete);
Thread newThread = new Thread(new
ParameterizedThreadStart(this.ThreadProc));
newThread.Start(dtx);

ts.Complete();
}
c1.Close();
}
}


private void ThreadProc(object o)
{
DependentTransaction dtx = (DependentTransaction)o;

using (SqlConnection c2 = new SqlConnection(@"Data Source=(local);Initial
Catalog=TXNTEST2;Integrated Security=True"))
{
using (TransactionScope ts2 = new TransactionScope(dtx))
{
c2.Open();
ts2.Complete();
}
dtx.Complete();
c2.Close();
}
}


The problem is when I attempt of open the second connection (c2) to the SAME
database an exception is thrown saying

"The operation is not valid for the state of the transaction."


Interestingly if I put a Thread.Sleep in the main thread before
ts.Complete() the open works ok and the transaction is promoted. This seems
to suggest that the ts.complete is doing something to the transaction to stop
the second connection enlisting. However I thought that the whole point of
the BlockCommitUntilComplete option is to stop the root TransactionScope
committing until any DependentTransaction processing is done.

Also, if I open another connection in the main thread (therefore promoting
the transaction) before I start the second thread then it also works.

Please could anyone shed any light on what is happening here, or if I am
doing something wrong.
 
D

David Browne

Boris72 said:
I have a problem using System.Transactions accross threads and would
welcome
any help.

My basic scenario is as follows:

Main Thread

-Create TransactionScope on main thread
-Open connection to SQL Server 2005 database
-Create Thread
-Start thread passing DependentTransaction
-Call TransactionScope.Complete

Worker Thread

-Create TransactionScope (using DependentTransaction)
-Open connection to SAME Database
-Call TransactionScope.Complete
-Call DependentTransaction.Complete

The code for this is below:

public void ThreadedSystemTransactions()
{
using (SqlConnection c1 = new SqlConnection(@"Data Source=(local);Initial
Catalog=TXNTEST2;Integrated Security=True"))
{
using (TransactionScope ts = new TransactionScope())
{
c1.Open();

DependentTransaction dtx =
Transaction.Current.DependentClone(DependentCloneOption.BlockCommitUntilComplete);
Thread newThread = new Thread(new
ParameterizedThreadStart(this.ThreadProc));
newThread.Start(dtx);

ts.Complete();
}
c1.Close();
}
}


private void ThreadProc(object o)
{
DependentTransaction dtx = (DependentTransaction)o;

using (SqlConnection c2 = new SqlConnection(@"Data Source=(local);Initial
Catalog=TXNTEST2;Integrated Security=True"))
{
using (TransactionScope ts2 = new TransactionScope(dtx))
{
c2.Open();
ts2.Complete();
}
dtx.Complete();
c2.Close();
}
}


The problem is when I attempt of open the second connection (c2) to the
SAME
database an exception is thrown saying

"The operation is not valid for the state of the transaction."


Interestingly if I put a Thread.Sleep in the main thread before
ts.Complete() the open works ok and the transaction is promoted. This
seems
to suggest that the ts.complete is doing something to the transaction to
stop
the second connection enlisting. However I thought that the whole point of
the BlockCommitUntilComplete option is to stop the root TransactionScope
committing until any DependentTransaction processing is done.

Also, if I open another connection in the main thread (therefore promoting
the transaction) before I start the second thread then it also works.

Please could anyone shed any light on what is happening here, or if I am
doing something wrong.

IMO you're swimming upstream. TransactionScope is an thread-based concept.
Do you really need the transaction started in the main thread?

David
 
T

TerryFei

Hi Boris,
Welcome to MSDN Newsgroup!

Based on my knowledge, TransactionScope can't be operated in two or more
threads. As a workaround for this issue, I suggest you should put all
operation about TransactionScope in a single thread.

I hope the above information is helpful for you. Thanks and have a nice day!

Best Regards,

Terry Fei [MSFT]
Microsoft Community Support
Get Secure! www.microsoft.com/security


--------------------
 

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