ADO.NET中提供了连接池的功能,多数开发人员很少设置它,因为它是默认的。
界面设置如下图:
关闭连接池也很简单,在连接字符串如下:
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 ._poolGroup = group; this ._innerConnection = DbConnectionClosedNeverOpened.SingletonInstance; } } if ( ! allowSetConnectionString) { throw ADP.OpenConnectionPropertySet( " ConnectionString " , innerConnection.State); } if (Bid.TraceOn) { string str = (userConnectionOptions != null ) ? userConnectionOptions.UsersConnectionStringForTrace() : "" ; Bid.Trace( " %d#, '%ls'\n " , this .ObjectID, str); }}
再连接 到红色的GetConnectionPoolGroup方法,如下代码
internal DbConnectionPoolGroup GetConnectionPoolGroup( string connectionString, DbConnectionPoolGroupOptions poolOptions, ref DbConnectionOptions userConnectionOptions){ DbConnectionPoolGroup group; if (ADP.IsEmpty(connectionString)) { return null ; } if ( ! this ._connectionPoolGroups.TryGetValue(connectionString, out group) || (group.IsDisabled && (group.PoolGroupOptions != null ))) { DbConnectionOptions options = this .CreateConnectionOptions(connectionString, userConnectionOptions); if (options == null ) { throw ADP.InternalConnectionError(ADP.ConnectionError.ConnectionOptionsMissing); } string str = connectionString; if (userConnectionOptions == null ) { userConnectionOptions = options; str = options.Expand(); if (str != connectionString) { return this .GetConnectionPoolGroup(str, null , ref userConnectionOptions); } } if ((poolOptions == null ) && ADP.IsWindowsNT) { if (group != null ) { poolOptions = group.PoolGroupOptions; } else { poolOptions = this .CreateConnectionPoolGroupOptions(options); } } DbConnectionPoolGroup group2 = new DbConnectionPoolGroup(options, poolOptions) { ProviderInfo = this .CreateConnectionPoolGroupProviderInfo(options) }; lock ( this ) { Dictionary dictionary = this ._connectionPoolGroups; if ( ! dictionary.TryGetValue(str, out group)) { Dictionary dictionary2 = new Dictionary( 1 + dictionary.Count); foreach (KeyValuePair pair in dictionary) { dictionary2.Add(pair.Key, pair.Value); } dictionary2.Add(str, group2); this .PerformanceCounters.NumberOfActiveConnectionPoolGroups.Increment(); group = group2; this ._connectionPoolGroups = dictionary2; } return group; } } if (userConnectionOptions == null ) { userConnectionOptions = group.ConnectionOptions; } return group;}
TryGetValue是判断是否存在连接字符串为connectionString的连接,存在返回到group,不存在就调用CreateConnectionOptions创建一个DbConnectionOptions,最后用
lock ( this ) { Dictionary dictionary = this ._connectionPoolGroups; if ( ! dictionary.TryGetValue(str, out group)) { Dictionary dictionary2 = new Dictionary( 1 + dictionary.Count); foreach (KeyValuePair pair in dictionary) { dictionary2.Add(pair.Key, pair.Value); } dictionary2.Add(str, group2); this .PerformanceCounters.NumberOfActiveConnectionPoolGroups.Increment(); group = group2; this ._connectionPoolGroups = dictionary2; } return group; }
这段代码放到连接池中,在这里,可能显示的看到,ado.NET的连接池实质上是一个Dictionary泛型集合。
所谓的连接池,就是一个与连接对象Connection相关的集合,这不只是简单的集合,而是有一定的机制在内部。我们做开发时,可能建立Connection连接对象,关闭连接对象,有时候还调用Dispose来释放连接。下次再用时,便重新实例化一个连接。但在池中的连接不随连接对象的Close或Dispose而释放。如果下次重新建立连接,连接字符串与前一次完全一模一样,则连接池就会把上次可用的连接对象赋给连接去用。如果两个连接字符串有一点不一样,即使在某一个地方多一个空格,连接池也不会以为是相同的连接,这点微软可能在内部只直接去比较两个字符串了,而不是比较连接数据库字符串的键值互相匹配。
连接池的好处就是保留连接对象,防止下次重头再来实例化一个连接对象。
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);Type SqlConType = null ; foreach (Type conType in ass.GetExportedTypes()) { Console.WriteLine(conType .ToString ()); if ( " System.Data.SqlClient.SqlConnection " == conType.ToString()) { SqlConType = conType; } } if (SqlConType != null ) { 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 ); }
(说明:可能找到结果后觉得非常简单,但怎么找到结果的,却是费了很大劲,几乎是5个小时,所以相把找到结果的过程简单说一下:
一开始用Reflector发现SqlConnection中有一个PoolGroup的属性,于是就想在运行时候比较两个SqlConnection对象的这个属性,但由于这个属性是的访问修饰符是internal的,不能直接访问,只有用反射,代码(是经过优化的)如下:
然后在倒数第一行设置断点,为比较poolGroup1和poolGroup2的不同,结果发现,当连接字符串一样时,这两个对象的_objectID相同,字符串有一点不同就会不同,这点说明连接池中是用字符串本身比较的,而不是字符串中键值对进行比较。同还发现当con1和con2的ConnectionString不赋值时这两个对象都是null,由此说明关键是ConnectionString赋值上,所以才开始用Reflector查看这个属性的赋值方法,才有上面的代码。)