473,804 Members | 3,597 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 = TransactionOpti on.Required;
ServiceDomain.E nter(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(CTe st1.EseguiOp));
workThread1.Sta rt();

Thread workThread2 = new Thread (new ThreadStart(CTe st2.EseguiOp));
workThread2.Sta rt();

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

is not automatically enlisted in the (Distributed)Tr ansaction 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.Enterpri seServices;
using System.Data.Sql Client;

namespace ClasseChiamata
{
public class ClasseTest
{

public bool hasError = false;
private SqlConnection mySqlConnection ;

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

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

SqlCommand mySqlCommand = new SqlCommand();

mySqlConnection .Open();
//mySqlConnection .EnlistDistribu tedTransaction( trans);

mySqlCommand.Co nnection = mySqlConnection ;
mySqlCommand.Co mmandText = "UPDATE Table1 SET Description = 'AAA' WHERE
Code = 'A1'";
mySqlCommand.Ex ecuteNonQuery() ;

ContextUtil.Set Abort(); // 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.Ex ception ex)
{
hasErrore = true;
}

}

}
}

the line that fails is the

ContextUtil.Set Abort(); // 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 2548
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 = TransactionOpti on.Required;
ServiceDomain.E nter(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(CTe st1.EseguiOp)); workThread1.Sta rt();

Thread workThread2 = new Thread (new
ThreadStart(CTe st2.EseguiOp)); workThread2.Sta rt();

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

is not automatically enlisted in the (Distributed)Tr ansaction 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, multithreadedap artments. 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
2195
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 ideas ?? Tks. ------------------------------------ Imports System.EnterpriseServices
1
2203
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 would happen where I have a stored proc with input parameters and 2 Delete commands on 2 different tables. In this case, would MSDTC also enforce ACID rules when 1 cmd succeeds and 2nd aborts ? In the script for the Stored Proc, there is no checking...
3
2932
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 Specifically: I have a web service that saves data to a database. This service uses a CommittableTransaction to decide when to Commit or Abort the underlying
6
2010
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 error on them now. The error occurs when I attempt a series of SQL statements wrapped in a TransactionScope(). It's executing against a different server, so this is where it's elevated to a distributed transaction.
0
9579
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10575
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10330
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10076
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5520
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4297
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3816
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2990
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.