在一次项目中,使用了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语句时启动!
参考文章:
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
最后还是解决不了,只好动了大工程,用单例模式处理了数据库连接字符串,然后再对其序列化。
再对数据层重新调整了下!
问题最终还是解决了!