question

TakuyaYamauchi-1944 avatar image
0 Votes"
TakuyaYamauchi-1944 asked TakuyaYamauchi-1944 commented

Can the DBConnection class be connected to by multiple STA threads in parallel?

When multiple STA threads try to access the DB using the DBConncetion class, all but the first one will not return a Connet function.
When the program is stopped, the locked thread will get an exception saying "COM object that has been separated from its underlying RCW cannot be used."

Can the DBConncetion class only be used from an MTA thread?

dotnet-adonet
· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi DanielZhang-MSFT, Thanks for the reply.
Sorry if I'm not giving you enough information, but in our program, the creation of the connection, the creation of the command, and the execution of the command are all running in the same thread.
I think we are able to use it the way you answered.

The only problem is that connections are created in multiple threads that are marked as STA in the program, and when the Open of these multiple connections is executed at the same time, the Open may not come back.

Is there any information on this?

0 Votes 0 ·

Hi @TakuyaYamauchi-1944,
Yes, you cannot use the same connection object in all threads. Give each thread a dedicated database connection.
More information about STAThread, please refer to this thread.
Best Regards,
Daniel Zhang


0 Votes 0 ·

Hi @DanielZhang-MSFT,
I thought I was using a different connection object in every thread.
But does that mean that the same connection object is actually being used?

Our program, written in C#, creates a connection for each STA thread as follows.
Then we use this connection in the same thread and Dispose it after it is used.

 _connection = new OleDbConnection(connectionString);
 _connection.Open();
 // ...
 _connection.Close();
 _connection.Dispose();

So when I process from these multiple STA threads, _connection.Open(); doesn't come back.
Does this mean that multiple connections can't be used from multiple threads?

0 Votes 0 ·
Show more comments

1 Answer

DanielZhang-MSFT avatar image
0 Votes"
DanielZhang-MSFT answered

Hi TakuyaYamauchi-1944,
Connections, commands, and readers are not thread-safe. Therefore, if you intend to run this code in multiple threads, each thread needs to create its own connections, commands, and readers. And if your application is multi-threaded, you need to use it in each The COM object is instantiated in the thread.
The STA model is used for non-thread-safe COM objects. If the COM object can handle its own synchronization, you can use the MTA model, which allows multiple threads to interact with the object without marshalling calls.
Here is a thread to help you understand better.
Best Regards,
Daniel Zhang


If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.