CommandBehavior.CloseConnection的使用

這是一個關於實際知識點的問題,面試官考查的是應聘者數據庫訪問的編程經驗。本節將針對這個問題展開具體的分析。對於此類關於具體知識點的問題,讀者在平時應該注意積累,這樣在面試中才能從容應答。

所涉及的知識點

CommandBehavior.CloseConnection的使用

分析問題

由於流模式讀取數據庫的特點,在具體應用時很難確定數據庫連接何時才能被關閉,因爲讀取的動作是連續進行的,下面是一個常見的數據訪問層的靜態方法:

/// <summary>
/// 常見的獲取SqlDataReader方法
/// 通常的數據訪問層都會提供這個方法
/// </summary>
static SqlDataReader GetReader()
{
//通過連接字符串獲取連接
SqlConnection con = new SqlConnection(conn_String);
try
{
//打開連接,執行查詢
//並且返回SqlDataReader
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = Sql;
SqlDataReader dr = cmd.ExecuteReader();
return dr;
}
finally
{
//這裏的代碼處於兩難的境地
//如果這裏執行關閉:con.Close();那返回的
SqlDataReader將毫無用處,因爲其
//依賴的連接已經關閉
//如果這裏不執行con.Close();那返回後該連接
將永遠無法關閉,因爲調用方無法
//得到連接對象
}
}

正如代碼註釋裏描述的那樣,這樣的方法既不能關閉連接,也不能保持連接打開狀態。很多系統爲了解決這樣兩難的境地,只能放棄使用Reader模式的數據源,或者把連接對象交給方法調用者,以便進行關閉。

而CommandBehavior.CloseConnection的功能恰好就是爲了避免類似的尷尬境地,它能夠保證當SqlDataReader對象被關閉時,其依賴的連接也會被自動關閉。代碼9-2展示了使用CommandBehavior.CloseConnection和不使用CommandBehavior.CloseConnection的區別。

這裏以SqlDataReader爲例進行說明,對於其他命名空間下的XXXDataReader對象,其功能是類似的。

首先爲了展示功能,代碼9-2包含了兩個靜態的返回SqlDataReader的方法,其中一個在執行ExecuteReader方法時傳入了CommandBehavior.CloseConnection方法。

代碼9-2 使用CommandBehavior.CloseConnection:UseCommandBehavior.cs

partial class UseCommandBehavior
{
//數據庫看連接字符串
const String conn_String = 
"Server=localhost;Integrated Security=true;database=NetTest";
const String Sql = "select * from dbo.DepartCost";          
/// <summary>
/// 使用CommandBehavior.CloseConnection
/// </summary>
/// <param name="con">爲了測試需要,傳入連接對象</param>
static SqlDataReader GetReader_CloseConnection(SqlConnection con)
{
try
{
//打開連接,執行查詢
//並且返回SqlDataReader
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = Sql;
SqlDataReader dr = cmd.ExecuteReader
(CommandBehavior.CloseConnection);
return dr;
}
finally
{
//因爲使用了CommandBehavior.CloseConnection,
//這裏不需要關閉連接
//con.Close();
}
}
/// <summary>
/// 不使用CommandBehavior.CloseConnection
/// </summary>
/// <param name="con">爲了測試需要,傳入連接對象</param>
static SqlDataReader GetReader_NoCloseConnection(SqlConnection con)
{
try
{
//打開連接,執行查詢
//並且返回SqlDataReader
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = Sql;
SqlDataReader dr = cmd.ExecuteReader();
return dr;
}
finally
{
//爲了使返回的SqlDataReader可用,這裏不能關閉連接
//con.Close();
}
}
}

可以看到,無論是否使用CommandBehavior.CloseConnection,兩個方法都沒有在最終關閉連接,但是它們不關閉連接的原因並不相同。準備好了兩個方法之後,就從主方法中分別調用這兩個方法來進行測試,以查看從使用了CommandBehavior.CloseConnection的方法中返回的SqlDataReader對象是否在關閉的同時自動關閉連接,如代碼9-3所示。

代碼9-3 使用CommandBehavior.CloseConnection:UseCommandBehavior.cs

partial class UseCommandBehavior
{
/// <summary>
/// 測試方法
/// </summary>
static void Main(string[] args)
{
//建立連接
SqlConnection con = new SqlConnection(conn_String);
try
{
//測試使用了CommandBehavior.CloseConnection的方法
Console.WriteLine("測試使用了CommandBehavior.
CloseConnection的方法:");
SqlDataReader sdr = GetReader_CloseConnection(con);
while (sdr.Read()) { }
sdr.Close();
Console.WriteLine("讀取完畢後的連接狀態:" + con.State.ToString());
//測試沒有使用CommandBehavior.CloseConnection的方法
Console.WriteLine("測試沒有使用CommandBehavior.
CloseConnection的方法:");
SqlDataReader sdr1 = GetReader_NoCloseConnection(con);
while (sdr1.Read()) { }
sdr1.Close();
Console.WriteLine("讀取完畢後的連接狀態:" + 
con.State.ToString());
Console.Read();
}
finally
{
//確保連接被關閉
if (con.State != ConnectionState.Closed)
con.Close();
}
}
}

下面是代碼的執行結果:

測試使用了CommandBehavior.CloseConnection的方法:

讀取完畢後的連接狀態:Closed

測試沒有使用CommandBehavior.CloseConnection的方法:

讀取完畢後的連接狀態:Open

正如讀者所看到的,使用了CommandBehavior.CloseConnection得到的SqlDataReader對象,在關閉的同時會自動地關閉其依賴的數據庫連接對象,這個特性解決了數據訪問層編寫中的困境。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章