Note that there are some explanatory texts on larger screens.

plurals
  1. POC# Question about thread safety
    primarykey
    data
    text
    <p>I'm using a threadpool to do some heavy processing and also bits of sql. Currently I open sql connections when I need them, run a query and then close them. This works fine. The application has been running with no issues. As more work is being done by this application it's using more threads. More threads mean more opening/closing of SQL connections. In SQL 2005 this actually hammers the server. My test server is doing around 175 transactions / sec. Approx 150 of these are being run in the master database and are "ValidateSQLLogin".</p> <p>I'm going to change the app so that each thread has it's own connection and then this connection is passed around the thread.</p> <p>So my question is:</p> <p>If a SQL connection object is created locally in a thread and is then passed by ref to a static function of another class would this be unsafe?</p> <pre><code>void ThreadA() { SqlConnection a = new SqlConnection(....); MyStaticClass.DoStuff(ref a); } void ThreadB() { SqlConnection b = new SqlConnection(....); MyStaticClass.DoStuff(ref b); } static void MyStaticClass.DoStuff(ref SqlConnection sql) { // Do stuff with sql } </code></pre> <p>My initial thought is that it would be unsafe as 10 threads could all call the same static function at the same time, each passing their own connection object.</p> <p>Previously the static functions opened their own connections and closed them when they were done.</p> <p>If it is unsafe whats the best method to work round this. I need to try and minimize open/close of Sql connections.</p> <p>Thanks</p> <p>Gareth</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload