CommandBehavior.CloseConnection有何作用

來源:互聯網
上載者:User
這是一個關於實際知識點的問題,面試官考查的是應聘者資料庫訪問的編程經驗。本節將針對這個問題展開具體的分析。對於此類關於具體知識點的問題,讀者在平時應該注意積累,這樣在面試中才能從容應答。所涉及的知識點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對象,在關閉的同時會自動地關閉其依賴的資料庫連接對象,這個特性解決了資料訪問層編寫中的困境。答案CommandBehavior.CloseConnection解決了流讀取資料模式下,資料庫連接不能有效關閉的情況。當某個XXXDataReader對象在產生時使用了CommandBehavior.CloseConnection,那資料庫連接將在XXXDataReader對象關閉時自動關閉。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.