Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sharing a db connection between threads in a C# application?

I have found there to be very little information on this topic, was hoping someone could direct me to some information and possible sample code.

like image 758
sam Avatar asked Dec 01 '09 21:12

sam


2 Answers

Generally connections are not thread safe(SqlConnection ,MySqlConnection and OracleConnection specifically mention that they are not thread safe).

Don't share a connection between threads.

like image 106
nos Avatar answered Oct 21 '22 12:10

nos


I would say dont share the connection object itself, just create a new connection and let ADO.net handle connection pooling.

like image 35
CSharpAtl Avatar answered Oct 21 '22 13:10

CSharpAtl