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.
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.