CommandBehavior.CloseConnection的使用

 
這是一個關於實際知識點的問題,面試官考查的是應聘者數據庫訪問的編程經驗。本節將針對這個問題展開具體的分析。對於此類關於具體知識點的問題,讀者在平時應該注意積累,這樣在面試中才能從容應答。
所涉及的知識點
CommandBehavior.CloseConnection的使用
分析問題
因爲流模式讀取數據庫的特色,在具體應用時很難肯定數據庫鏈接什麼時候才能被關閉,由於讀取的動做是連續進行的,下面是一個常見的數據訪問層的靜態方法:
/// <summary>/// 常見的獲取SqlDataReader方法/// 一般的數據訪問層都會提供這個方法/// </summary>static SqlDataReader GetReader(){//經過鏈接字符串獲取鏈接SqlConnection con = new SqlConnection(conn_String);try{//打開鏈接,執行查詢//而且返回SqlDataReadercon.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{//打開鏈接,執行查詢//而且返回SqlDataReadercon.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{//打開鏈接,執行查詢//而且返回SqlDataReadercon.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對象,在關閉的同時會自動地關閉其依賴的數據庫鏈接對象,這個特性解決了數據訪問層編寫中的困境。
答案
CommandBehavior.CloseConnection解決了流讀取數據模式下,數據庫鏈接不能有效關閉的狀況。當某個XXXDataReader對象在生成時使用了CommandBehavior.CloseConnection,那數據庫鏈接將在XXXDataReader對象關閉時自動關閉。
 
本文來自CSDN博客,轉載請標明出處: http://blog.csdn.net/guopengzhang/archive/2009/11/07/4782786.aspx
相關文章
相關標籤/搜索