Data Source=(local);Initial Catalog=AdventureWorks;Integrated Security=SSPI;Pooling=False
; 用Reflector
,打开System.Data.SqlClient.SqlConnection
的ConnectionString
属性的设置值的方法,如下: private void ConnectionString_Set(string value)
DbConnectionOptions userConnectionOptions = null;
DbConnectionPoolGroup group = this.ConnectionFactory.
GetConnectionPoolGroup(value, null, ref userConnectionOptions);
DbConnectionInternal innerConnection = this.InnerConnection;
bool allowSetConnectionString = innerConnection.AllowSetConnectionString;
if (allowSetConnectionString)
allowSetConnectionString= this.SetInnerConnectionFrom(DbConnectionClosedBusy.SingletonInstance, innerConnection);
if (allowSetConnectionString)
this._userConnectionOptions = userConnectionOptions;
this._innerConnection = DbConnectionClosedNeverOpened.SingletonInstance;
if (!allowSetConnectionString)
throw ADP.OpenConnectionPropertySet("ConnectionString", innerConnection.State);
string str = (userConnectionOptions != null) ? userConnectionOptions.UsersConnectionStringForTrace() : "";
Bid.Trace("<prov.DbConnectionHelper.ConnectionString_Set|API> %d#, '%ls'\n", this.ObjectID, str);
再连接 到红色的GetConnectionPoolGroup
方法,如下代码 internal DbConnectionPoolGroup GetConnectionPoolGroup(string connectionString, DbConnectionPoolGroupOptions poolOptions, ref DbConnectionOptions userConnectionOptions)
DbConnectionPoolGroup group;
if (ADP.IsEmpty(connectionString))
if (!this._connectionPoolGroups.
TryGetValue(connectionString, out group) || (group.IsDisabled && (group.PoolGroupOptions != null)))
DbConnectionOptions options =
this.CreateConnectionOptions(connectionString, userConnectionOptions);
throw ADP.InternalConnectionError(ADP.ConnectionError.ConnectionOptionsMissing);
string str = connectionString;
if (userConnectionOptions == null)
userConnectionOptions = options;
if (str != connectionString)
return this.GetConnectionPoolGroup(str, null, ref userConnectionOptions);
if ((poolOptions == null) && ADP.IsWindowsNT)
poolOptions = group.PoolGroupOptions;
poolOptions = this.CreateConnectionPoolGroupOptions(options);
DbConnectionPoolGroup group2 = new DbConnectionPoolGroup(options, poolOptions) {
ProviderInfo = this.CreateConnectionPoolGroupProviderInfo(options)
Dictionary<string, DbConnectionPoolGroup> dictionary = this._connectionPoolGroups;
if (!dictionary.TryGetValue(str, out group))
Dictionary<string, DbConnectionPoolGroup> dictionary2 = new Dictionary<string, DbConnectionPoolGroup>(1 + dictionary.Count);
foreach (KeyValuePair<string, DbConnectionPoolGroup> pair in dictionary)
dictionary2.Add(pair.Key, pair.Value);
dictionary2.Add(str, group2);
this.PerformanceCounters.NumberOfActiveConnectionPoolGroups.Increment();
this._connectionPoolGroups = dictionary2;
if (userConnectionOptions == null)
userConnectionOptions = group.ConnectionOptions;
TryGetValue
是判断是否存在连接字符串为connectionString
的连接,存在返回到group
,不存在就调用CreateConnectionOptions
创建一个DbConnectionOptions
,最后用 Dictionary<string, DbConnectionPoolGroup> dictionary = this._connectionPoolGroups;
if (!dictionary.TryGetValue(str, out group))
Dictionary<string, DbConnectionPoolGroup> dictionary2 = new Dictionary<string, DbConnectionPoolGroup>(1 + dictionary.Count);
foreach (KeyValuePair<string, DbConnectionPoolGroup> pair in dictionary)
dictionary2.Add(pair.Key, pair.Value);
dictionary2.Add(str, group2);
this.PerformanceCounters.NumberOfActiveConnectionPoolGroups.Increment();
this._connectionPoolGroups = dictionary2;
这段代码放到连接池中,在这里,可能显示的看到,ado.NET
的连接池实质上是一个Dictionary<string, DbConnectionPoolGroup>
泛型集合。 所谓的连接池,就是一个与连接对象Connection
相关的集合,这不只是简单的集合,而是有一定的机制在内部。我们做开发时,可能建立Connection
连接对象,关闭连接对象,有时候还调用Dispose
来释放连接。下次再用时,便重新实例化一个连接。但在池中的连接不随连接对象的Close
或Dispose
而释放。如果下次重新建立连接,连接字符串与前一次完全一模一样,则连接池就会把上次可用的连接对象赋给连接去用。如果两个连接字符串有一点不一样,即使在某一个地方多一个空格,连接池也不会以为是相同的连接,这点微软可能在内部只直接去比较两个字符串了,而不是比较连接数据库字符串的键值互相匹配。 连接池的好处就是保留连接对象,防止下次重头再来实例化一个连接对象。 (说明:可能找到结果后觉得非常简单,但怎么找到结果的,却是费了很大劲,几乎是5
个小时,所以相把找到结果的过程简单说一下: 一开始用Reflector
发现SqlConnection
中有一个PoolGroup
的属性,于是就想在运行时候比较两个SqlConnection
对象的这个属性,但由于这个属性是的访问修饰符是internal
的,不能直接访问,只有用反射,代码(是经过优化的)如下: string constr1 = "Data Source=(local);Initial Catalog=AdventureWorks;Integrated Security=SSPI;"; string constr2 = "Data Source=(local);Initial Catalog=Pubs;Integrated Security=SSPI;"; string AssMark = "System.Data,Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"; Assembly ass = Assembly.Load(AssMark); foreach (Type conType in ass.GetExportedTypes()) Console.WriteLine(conType .ToString ()); if ("System.Data.SqlClient.SqlConnection" == conType.ToString()) Type[] types1 = new Type[0]; ConstructorInfo constructorInfoObj1 = SqlConType.GetConstructor( BindingFlags.Instance | BindingFlags.Public, null, CallingConventions.HasThis, types1, null); SqlConnection con1 = (SqlConnection)constructorInfoObj1.Invoke(null); con1.ConnectionString = constr1; SqlConnection con2 = (SqlConnection)constructorInfoObj1.Invoke(null); con2.ConnectionString = constr2; PropertyInfo PI = SqlConType.GetProperty("PoolGroup", BindingFlags.Instance | BindingFlags.NonPublic); object poolGroup1 = PI.GetValue(con1, null); object poolGroup2 = PI.GetValue(con2, null); 然后在倒数第一行设置断点,为比较poolGroup1
和poolGroup2
的不同,结果发现,当连接字符串一样时,这两个对象的_objectID
相同,字符串有一点不同就会不同,这点说明连接池中是用字符串本身比较的,而不是字符串中键值对进行比较。同还发现当con1
和con2
的ConnectionString
不赋值时这两个对象都是null
,由此说明关键是ConnectionString
赋值上,所以才开始用Reflector
查看这个属性的赋值方法,才有上面的代码。) 本文转自桂素伟51CTO博客,原文链接:http://blog.51cto.com/axzxs/278442 ,如需转载请自行联系原作者