首页 新闻 会员 周边 捐助

谁看看这段代码有问题没?

0
悬赏园豆:10 [已关闭问题]
<P>无意间在<A class="" href="http://www.google.com/codesearch?hl=en&amp;q=+lang:c%23+SQLiteFactory+show:cb5wnwNx4V4:snjGWivFAH8:akJ7Y2rgnmo&amp;sa=N&amp;cd=6&amp;ct=rc&amp;cs_p=http://buktools.googlecode.com/svn&amp;cs_f=trunk/.NET/BukTools/src/Buk.Core/Data/DatabaseHelper.cs" mce_href="http://www.google.com/codesearch?hl=en&amp;q=+lang:c%23+SQLiteFactory+show:cb5wnwNx4V4:snjGWivFAH8:akJ7Y2rgnmo&amp;sa=N&amp;cd=6&amp;ct=rc&amp;cs_p=http://buktools.googlecode.com/svn&amp;cs_f=trunk/.NET/BukTools/src/Buk.Core/Data/DatabaseHelper.cs">google codesearch</A>&nbsp;&nbsp;上看到这样一段代码:<BR>其中有个ExecuteReader方法,如下:<BR>public DbDataReader ExecuteReader(string query,CommandType commandtype, ConnectionState connectionstate)<BR><SPAN id=l405>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR></SPAN><SPAN id=l406>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; objSelectOrQueryCommand.CommandText = query;<BR></SPAN><SPAN id=l407>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; objSelectOrQueryCommand.CommandType = commandtype;<BR></SPAN><SPAN id=l408>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DbDataReader reader=null;<BR></SPAN><SPAN id=l409>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try<BR></SPAN><SPAN id=l410>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR></SPAN><SPAN id=l411>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (objConnection.State == System.Data.ConnectionState.Closed)<BR></SPAN><SPAN id=l412>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR></SPAN><SPAN id=l413>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; objConnection.Open();<BR></SPAN><SPAN id=l414>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR></SPAN><SPAN id=l415>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (connectionstate == ConnectionState.CloseOnExit)<BR></SPAN><SPAN id=l416>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR></SPAN><SPAN id=l417>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; reader = objSelectOrQueryCommand.ExecuteReader(CommandBehavior.CloseConnection);<BR></SPAN><SPAN id=l418>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR></SPAN><SPAN id=l419>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else<BR></SPAN><SPAN id=l420>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb
问题补充: { reader = objSelectOrQueryCommand.ExecuteReader(); } } catch (Exception ex) { HandleExceptions(ex); } finally { objSelectOrQueryCommand.Parameters.Clear(); } return reader; } HandleExceptions方法如下:(此异常处理中,如果LogErrors,HandleErrors为true的话,DataReader的连接不是就没有机会关闭了吗?会不会造成链接泄漏??) private void HandleExceptions(Exception ex) { string exMsg = ex.Message + Environment.NewLine + ex.StackTrace; if (LogErrors) { WriteToLog(exMsg); } if (HandleErrors) { strLastError = exMsg; } else { throw ex; } }
钱小柜的主页 钱小柜 | 初学一级 | 园豆:190
提问于:2008-01-23 17:55
< >
分享
其他回答(1)
0
这个时间应该是打开链接失败的情况吧?何来泄漏之说
沙加 | 园豆:3680 (老鸟四级) | 2008-01-23 21:34
0
这里还没有到处理DataReader开关的时候,应该不会存在泄漏的问题。 reader = objSelectOrQueryCommand.ExecuteReader(CommandBehavior.CloseConnection); reader释放 就会自动关闭Connection! 根据这段代码 他维护的Connection对象应该一直是一个objConnection,也就是说command对象objSelectOrQueryCommand使用的connection一直是objConnection,每次都是判断这个对象,应该不会产生新的Connection的,也就不会产生泄漏:)。
lu xu | 园豆:410 (菜鸟二级) | 2008-01-24 10:02
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册