title: Is SqlConnection / SqlCommand thread safe?
body: <p>I am currently creating a WCF web service.</p>
  
  <p>As part of its job, it will unfortunately need to do some fairly intensive computations, however these computations can fortunately be shared between calls to the webservice. In effect - we only need to do the computations once, and all later calls can get the benefit.</p>
  
  <p>However, since WCF has no shared application state it seems logical to set WCF in single-instance mode. (Each client would require some of the computations in all likelyhood, forcing us to recompute them per-serssion which <em>could</em> be ok, or per-call which is untenable)</p>
  
  <p>However, I am not very familiar with securing code for multiple threads. 
  I have been reading up on it some, and as none of our WCF code writes to shared state (other than the computation-bit, which is easy to protect) I'm almost convinced I don't need to change anything. </p>
  
  <p>There is a single snag, though - we use SqlConnection and SqlCommand to communicate with our backend, and I am not sure if I can count on these being thread safe?</p>
  
  <p>EDIT: I should perhaps clarify that the Commands / Connections are always local to a method. We're talking a pattern in the vein of:</p>
  
  <pre><code>using sqlConn = new SqlConnection(...) {
   try {
    sqlConn.Open()
  } catch () {
    throw new FaultException();
  }
  var cmd = new SqlCommand("Some SQL", sqlConn);
  var reader = cmd.ExecuteReader();
  //Read the stuff 
  reader.Close();
  //Return something
  }
  </code></pre>
  
  <p>END EDIT</p>
  
  <p>I looked up the SqlCommand class on MSDN:
  <a href="http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.aspx" rel="noreferrer">http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.aspx</a>
  which says: "Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe."</p>
  
  <p>Am I interpreting this correctly in thinking it means that MS does <em>not</em> guarantee that SqlCommand works in a multi threaded scenario? </p>
  
  <p>If it does not, is there a thread-safe alternative?</p>
  
  <p>Yes, I could just lock all database access methods in my webservice, but a) it's ugly and b) if it's not necessary I'd prefer I didn't have to :)</p>
  
  <p>Cheers in advance!</p>