ADO.NET中提供了連接配接池的功能,多數開發人員很少設定它,因為它是預設的。
界面設定如下圖:
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIn5GcuIGMlVGNzMmN0UTZmVjY1YWZmNTM4QmNyQDNzADM1ITMfdWbp9CXt92Yu4GZjlGbh5SZslmZxl3Lc9CX6MHc0RHaiojIsJye.png)
關閉連接配接池也很簡單,在連接配接字元串如下:
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);
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;
userConnectionOptions = group.ConnectionOptions;
TryGetValue是判斷是否存在連接配接字元串為connectionString的連接配接,存在傳回到group,不存在就調用CreateConnectionOptions建立一個DbConnectionOptions,最後用
這段代碼放到連接配接池中,在這裡,可能顯示的看到,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=b77a5c561
934e089";
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檢視這個屬性的指派方法,才有上面的代碼。)