Frage

What is the behavior of SqlConnection.ClearPool if I pass as argument a connection used within a transaction (for example using a System.Transactions.TransactionScope)?

For example, in this snippet

using(var tx=new System.Transactions.TransactionScope())
{
  var sqlConn=new System.Data.SqlClient.SqlConnection(...);
  /*do something with connection*/

  System.Data.SqlClient.SqlConnection.ClearPool(sqlConn);

  tx.Complete();
}

with the ClearPool call am I clearing all the connections with the same connection string (regardless of transaction) or in fact am I closing only the connection I'm passing (because it is "associated" with the transaction)?

I searched on the Internet but I found nothing. From MSDN (here and here) it's clear the behavior without transaction; moreover I understand that if I'm in a transaction the connection pool tries to give to me the connection I used before in that transaction (if it still exists). But it is not clear to me the behavior of ClearPool when called with a connection used in a transaction.

War es hilfreich?

Lösung

Since I haven't found any official documentation about this specific scenario, I've realized a small sample (available at https://github.com/edymtt/sqlclientconnectionpool) that simulates this scenario. The important thing to do before running this sample is to stop MS DTC on the test machine -- so that we don't deal with distributed transactions and we don't miss that a new SqlConnection is created.

This way I've discovered that SqlConnection.ClearPool clears all the connections that has the same connection string of the connection passed as arguments, regardless of any transaction associated to the connection.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top