詳解ADO.NET連接池

作者: 桂素偉  來源: 博客園  發布時間: 2010-02-26 10:24  閱讀: 3618 次  推薦: 1   原文鏈接   [收藏]  

ADO.NET中提供了連接池的功能,多數開發人員很少設置它,因為它是默認的。

界面設置如下圖:

 

 

 

關閉連接池也很簡單,在連接字符串如下:

Data Source=(local);Initial Catalog=AdventureWorks;Integrated Security=SSPI;Pooling=False;

但連接池的本質是什么樣的呢?

 

Reflector,打開System.Data.SqlClient.SqlConnectionConnectionString屬性的設置值的方法,如下:

 

代碼
 
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來釋放連接。下次再用時,便重新實例化一個連接。但在池中的連接不隨連接對象的CloseDispose而釋放。如果下次重新建立連接,連接字符串與前一次完全一模一樣,則連接池就會把上次可用的連接對象賦給連接去用。如果兩個連接字符串有一點不一樣,即使在某一個地方多一個空格,連接池也不會以為是相同的連接,這點微軟可能在內部只直接去比較兩個字符串了,而不是比較連接數據庫字符串的鍵值互相匹配。

連接池的好處就是保留連接對象,防止下次重頭再來實例化一個連接對象。

 

 
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的,不能直接訪問,只有用反射,代碼(是經過優化的)如下:

然后在倒數第一行設置斷點,為比較poolGroup1poolGroup2的不同,結果發現,當連接字符串一樣時,這兩個對象的_objectID相同,字符串有一點不同就會不同,這點說明連接池中是用字符串本身比較的,而不是字符串中鍵值對進行比較。同還發現當con1con2ConnectionString不賦值時這兩個對象都是null,由此說明關鍵是ConnectionString賦值上,所以才開始用Reflector查看這個屬性的賦值方法,才有上面的代碼。)

1
0
 
標簽:ADO.NET
 
 

文章列表

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()