title: Stateless WCF service and database connection pooling
body: <p>The question has been asked before here in StackOverflow, but in my experience, the answers were actually wrong. At least for .NET Framework 4.0 and SQL Server 2005 they are wrong. </p>
  
  <p>I would need help to sort this out once and for all. </p>
  
  <p>The question is - can a stateless WCF service use database connection pooling in some way?</p>
  
  <p>See <a href="https://stackoverflow.com/questions/2746128/can-a-stateless-wcf-service-benefit-from-built-in-database-connection-pooling">Can a Stateless WCF service ...</a></p>
  
  <p>The earlier answers essentially stated that there is no problem and no difference to any other ADO.NET scenarios. However, I have not been able to get a stateless WCF service to use the connection pooling EVER, while I can see it always work outside WCF services. No matter what connection strings or parameters I am trying to use, it does not do it.</p>
  
  <p>Database connection pooling is meant to be enabled by default, so a simple connection string should get me there, for instance on SQL Server Express:</p>
  
  <pre><code>SqlConnection sqlCn = new SqlConnection("Data Source=SERVER\SQLEXPRESS; Initial Catalog = xDB; Integrated Security = SSPI;")
  </code></pre>
  
  <p>Using this connection, in a Windows Form application, if I do 3 consecutive rounds of <code>sqlCn.Open()</code> -- query the database -- <code>sqlCn.Close()</code>, I am getting a long delay (for instance 2 seconds) on the first <code>sqlCn.Open()</code>, and no delays at all on queries and open / close afterwards. Exactly what I expect with database connection pooling.</p>
  
  <p>But if I make 3 calls to a WCF service containing the same <code>sqlCn.Open()</code> -- query the database -- <code>sqlCn.Close()</code> code, I am getting the 2 second initial slow startup for EVERY single call.</p>
  
  <p>My guess is that the connection pooling is entirely controlled by the ADO.NET objects created by my code, and since I am instantiating any ADO.NET classes I use (such as <code>SqlConnection</code> etc) inside my WCF service, they get destroyed when my service call is over and the connection pool along with it. </p>
  
  <p>This may not be true, but if not, is there anything wrong with what I have done?</p>
  
  <p>Anyone have any experience with that? </p>
  
  <p>(Please do test any assumption or theory before posting)</p>