473,396 Members | 2,039 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

MSDTC, Transaction, Context and Threads

I've a problem using multithreading and MTS.
I'm using framework 1.1

Basically I want, from the Main Thread , to start a (distributed)
transaction.
then I want to start 2 new threads to perform some Database operations, each
thread will do some work on the DB using its own SQLConnection.
I want the 2 SQLConnections of the two threads partecipate to the same
(distributed) transaction.
The problem is that i do not know how to propagate the Object Context from
the Main thread to the 2 subThreads.

I start a new Transactional Context from the Main Thread with the following
code:
sc = new ServiceConfig();
sc.Transaction = TransactionOption.Required;
ServiceDomain.Enter(sc);
Then I run two Threads that will open two distinct SQLConnections
to perform some work at the same time .
ClasseTest CTest1 = new ClasseChiamata.ClasseTest() ;
ClasseTest CTest2 = new ClasseChiamata.ClasseTest() ;

Thread workThread1 = new Thread (new ThreadStart(CTest1.EseguiOp));
workThread1.Start();

Thread workThread2 = new Thread (new ThreadStart(CTest2.EseguiOp));
workThread2.Start();

workThread1.Join();
workThread2.Join();
The problem is that the SQLConnection created inside the method:
CTest2.EseguiOp()

is not automatically enlisted in the (Distributed)Transaction created before
the methods have been invoked asynchronously.

All works fine if I invoke the method synchronously, while if I use
multithreading to recall
the same method, the MTS object Context is not available to the subThreads.

Do you know how to pass the MTS Context to the 2 subThreads?
this is the code of the method that I want to recall asynchronously:
using System;
using System.Data;
using System.EnterpriseServices;
using System.Data.SqlClient;

namespace ClasseChiamata
{
public class ClasseTest
{

public bool hasError = false;
private SqlConnection mySqlConnection;

public ClasseTest()
{
}
public void EseguiOp()
{
try
{

mySqlConnection = new SqlConnection
("database=TEST;server=localhost;Persist Security
Info=False;uid=sa;pwd=xxxx");

SqlCommand mySqlCommand = new SqlCommand();

mySqlConnection.Open();
//mySqlConnection.EnlistDistributedTransaction(trans );

mySqlCommand.Connection = mySqlConnection;
mySqlCommand.CommandText = "UPDATE Table1 SET Description = 'AAA' WHERE
Code = 'A1'";
mySqlCommand.ExecuteNonQuery();

ContextUtil.SetAbort(); // this will work in synchronous mode, but will
fail if recalled in a secondary thread, with the error "there is no MTS
object Context"!

}
catch(System.Exception ex)
{
hasErrore = true;
}

}

}
}

the line that fails is the

ContextUtil.SetAbort(); // this will work in synchronous mode, but will
fail if recalled in a secondary thread, with the error "there is no MTS
object Context"!
any help will be apreciated!

JPC
Feb 20 '06 #1
1 2524
Gian Paolo Clarici wrote:
I've a problem using multithreading and MTS.
I'm using framework 1.1

Basically I want, from the Main Thread , to start a (distributed)
transaction.
then I want to start 2 new threads to perform some Database
operations, each thread will do some work on the DB using its own
SQLConnection. I want the 2 SQLConnections of the two threads
partecipate to the same (distributed) transaction.
The problem is that i do not know how to propagate the Object Context
from the Main thread to the 2 subThreads.

I start a new Transactional Context from the Main Thread with the
following code:
sc = new ServiceConfig();
sc.Transaction = TransactionOption.Required;
ServiceDomain.Enter(sc);
Then I run two Threads that will open two distinct SQLConnections
to perform some work at the same time .
ClasseTest CTest1 = new ClasseChiamata.ClasseTest() ;
ClasseTest CTest2 = new ClasseChiamata.ClasseTest() ;

Thread workThread1 = new Thread (new
ThreadStart(CTest1.EseguiOp)); workThread1.Start();

Thread workThread2 = new Thread (new
ThreadStart(CTest2.EseguiOp)); workThread2.Start();

workThread1.Join();
workThread2.Join();
The problem is that the SQLConnection created inside the method:
CTest2.EseguiOp()

is not automatically enlisted in the (Distributed)Transaction created
before the methods have been invoked asynchronously.

All works fine if I invoke the method synchronously, while if I use
multithreading to recall
the same method, the MTS object Context is not available to the
subThreads.

Do you know how to pass the MTS Context to the 2 subThreads?
any help will be apreciated!

Your threads have to be marked as MTA, multithreadedapartments. You've
to check the ApartmentState of the thread objects. Still, I'm with you
that it's unclear how to do this. Browsing through the docs I ran into
quite a view inconsistencies, so I'm sorry but I can't give you a clear
answer, other than that the threading model of your threads has to be
MTA, not STA.

FB

--
------------------------------------------------------------------------
Get LLBLGen Pro, productive O/R mapping for .NET: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
Feb 21 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: MarcoB | last post by:
I am trying to use Microsoft Distributed Processing and I am getting the following error. "There is no MTS objectcontext " I have SQL MSDTC loaded on my PC. I have added the code below: Any...
1
by: sjoshi | last post by:
Hello I had a question reagrding MSDTC usage. I know that MSDTC will enlist each connection transactions as it's own (take ownership) and commit/abort accordingly. However I'm not sure what...
3
by: =?Utf-8?B?YWJheWVy?= | last post by:
In short: I have an application using the CommittableTransaction class that works fine when run locally, but does not escalate to MSDTC when deployed to a Windows 2003 R2 server. More...
6
by: =?Utf-8?B?UGF1bCBQcmV3ZXR0?= | last post by:
Hi - I have 4 webservers in my webfarm. All Win2k3 web edition. Before yesterday, none of them were service packed. I have now applied SP2 to two of them, and I'm getting a very weird MSDTC...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.