在一次項目中,使用了access資料庫。
本地測試無出現異常,但在伺服器上一重新整理就會跑出這個錯誤。
很明顯的錯誤就是資料庫沒有關閉,在使用DataReader時資源沒有及時釋放。[一般情況下]
於是找到資料庫操作類,卻很明顯的都已經及時釋放掉資源。一步步跟蹤調試也無發現錯誤源頭。
網上搜了相關問題 發現矛頭都是指向資源釋放問題。
因為在邏輯層用到了using語句來釋放一個或多個對象,在裡面就return了出來。
會不會是這個導致的呢?!
結果修改、測試後,發現還是會!但是網上搜到的資源提及是using在搞鬼!個人覺得不大可能!
後來真的證實了using語句是可以return 出來的。
e.g:
using(DataSet ds1 = new DataSet(),ds2 = new DataSet()){}
在using語句中使用return語句傳回值時,using仍然能夠保證釋放指定的對象。
不但如此,不管是正常程式流結束、使用return語句跳出還是拋出異常,using範圍內的對象都會正常析構。
在這一點上有點類似於finally,不過比finally更為及時,因為無需等待記憶體回收機制啟動的時刻,而是在退出using語句時啟動!
參考文章:
Return Within a C# Using Statement
While writing some code earlier today I needed to return from within a using statement. Doing these sorts of things always makes me appreciate the using statement and how wonderful it really is, so I decided to write about it here. As many of you know the using statement in C# is a good tool for managing types which will be accessing unmanaged resources. Some examples of these are SqlConnections, FileReaders, and plenty of other similar types. The key to these is that they all implement the IDisposable interface. This means that they all need to be cleaned up carefully after using them.
The using statement is great because it guarantees that the declared object is disposed no matter how the execution completes. Whether you reach the end curly brace marking the end of the using statement, throw and exception, or return from a function, the using statement will call the dispose method and clean up the object.
This was important in my code because I was able to return directly from within the using statement without worrying about whether or not eh dispose method will fire. Whenever I use an object which accesses unmanaged resources I always always always put it in a using statement.
It is very important to use a using statement, because it will give you this guarantee that the object will be disposed of correctly. The object's scope will be for the extent of the using statement, and during the scope of the object it will be read-only if defined in the using statement. This is also very nice, because it will prevent this important object which manages the unmanaged from being modified or reassigned.
This is safe to do, because of how great the using statement is. No matter which return we hit we know the XmlReader will be disposed of correctly.
using (XmlReader reader = XmlReader.Create(xmlPath))
{
// ... Do some work...
if (someCase)
return 0;
// ... Do some work...
if (someOtherCase)
return 1;
}
return -1;
連結:http://aspadvice.com/blogs/name/archive/2008/05/22/Return-Within-a-C_2300_-Using-Statement.aspx
最後還是解決不了,只好動了大工程,用單例模式處理了資料庫連接字串,然後再對其序列化。
再對資料層重新調整了下!
問題最終還是解決了!