若要知道数据库连接超时问题,先看下面一段代码: [Sample-01]: Public Shared Function getOEMPN(ByVal psPN As String, ByRef OEMPN As String) As BSResult 0001 Dim clsResult As New BSResult 0002 Try 0003 clsResult.ResultID = -1 0004 Dim dtResult As New DataTable 0005 Dim Sql As String = String.Empty 0006 Dim clsOraDb As New clsOraClienDb 0007 Dim strConn As String = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString 0008 clsOraDb.Open(strConn) ‘这里Open后,后面看不到 clsOraDb.Close 0009 Sql = "SELECT SATBMMBRND.OEMPN FRUNO FROM SATBMMBRND WHERE SATBMMBRND.MATNO = :MATNO" 0010 Dim params() As OracleParameter = {New OracleParameter("MATNO", psPN)} 0011 If clsOraDb.FillDataTable(Sql, dtResult, params) = False Then 0012 Return clsResult 0013 End If 0014 If dtResult Is Nothing Then 0015 Return clsResult 0016 End If 0017 If dtResult.Rows.Count > 0 Then 0018 OEMPN = dtResult.Rows(0)("FRUNO").ToString() 0019 Else 0020 OEMPN = "" 0021 End If 0022 clsResult.ResultID = 1 0023 Return clsResult 0024 Catch ex As Exception 0025 clsResult.ResultID = -1 0026 Return clsResult 0027 End Try End Function 对上述代码行的部分解释: 0006:引用数据库连接的类; 0008:打开数据库连接;
这还不算什么,更有甚者,尽然在循环语句里写下面的代码如 : [Sample-02] Foreach(DataRow row in tabl.select(“”,”ProductID”) …………… clsOraDb.Open(strConn) …………. Next 有人还喜欢玩下面的语句: [Sample-03] Foreach(DataRow row in tabl.select(“”,”ProductID”) Foreach(DataColumn col in tbl.columns) …………… clsOraDb.Open(strConn)
Next …………. Next 说到这,有人就问啦,我在开发环境下测试一点问题都没有呀?是呀,你是没有问题,我想问的是,你开发环境的测试数据有几笔?
现在,问题已经知道在哪里,怎么解决?
针对[sample-01]做如下处理,注意下面代码:
Public Shared Function getOEMPN(ByVal psPN As String, ByRef OEMPN As String) As BSResult 0001 Dim clsResult As New BSResult 0002 Try 0003 clsResult.ResultID = -1 0004 Dim dtResult As New DataTable 0005 Dim Sql As String = String.Empty 0006 Dim clsOraDb As New clsOraClienDb 0007 Dim strConn As String = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString 0008 clsOraDb.Open(strConn) 注释:这里Open后,后面看不到 clsOraDb.Close 0009 Sql = "SELECT SATBMMBRND.OEMPN FRUNO FROM SATBMMBRND WHERE SATBMMBRND.MATNO = :MATNO" 0010 Dim params() As OracleParameter = {New OracleParameter("MATNO", psPN)} 0011 If clsOraDb.FillDataTable(Sql, dtResult, params) = False Then 0012 Return clsResult 0013 End If 0014 If dtResult Is Nothing Then 0015 Return clsResult 0016 End If 0017 If dtResult.Rows.Count > 0 Then 0018 OEMPN = dtResult.Rows(0)("FRUNO").ToString() 0019 Else 0020 OEMPN = "" 0021 End If 0022 clsResult.ResultID = 1 0088 clsOraDb.Close 注释:后面看到 clsOraDb.Close 0023 Return clsResult 0024 Catch ex As Exception 0099 clsOraDb.Close 注释:程序异常也看到 clsOraDb.Close 0025 clsResult.ResultID = -1 0026 Return clsResult 0028 Throw ex 0027 End Try End Function 注意上面的两句代码:0088行和0099行。
在异常处理的时候,特别提醒两点:
一, 你的数据库关闭的时候应该是在代码行0028前,而不是后;
二, 有人不习惯(或者一时疏忽)加上0088行的代码;
针对[Sample-02]和[sample-03],把打开数据库连接写在所有的循环语句之前,如:
clsOraDb.Open(strConn) Foreach(DataRow row in tabl.select(“”,”ProductID”) ……………
…………. Next 当然还有另外一个做法,就是用Using语 二、对象只管创建应用,不管释放篇
我们继续用[Sample-01]的代码,我们现在看0004行的代码: 0004 Dim dtResult As New DataTable 谁会发现它被释放,你不能,我也不能,从来没有被释放过。
Dim dtResult As New DataTable Dim DR as New DataReader Dim DS as New Dataset Try .. Catch ex As Exception Throw ex Finally End Try
2.2释放的语句如下
Dim dtResult As New DataTable Dim DR as New DataReader Dim DS as New Dataset Try .. …………….. Catch ex As Exception --释放 应用的对象 Throw ex Finally --使用完后,释放应用的对象 dtResult.dispose --从内存里清楚该对象 DR.dispose -从内存里清楚该对象 DS.dispose -从内存里清楚该对象 End Try 有 人习惯写成下面这样: Dim dtResult As New DataTable Dim DR as New DataReader Dim DS as New Dataset Try .. ‘使用完后,释放应用的对象 dtResult.dispose ‘从内存里清楚该对象 DR.dispose ‘从内存里清楚该对象 DS.dispose ‘从内存里清楚该对象 Catch ex As Exception ‘释放应用的对象 Throw ex Finally End Try
Foreach (DataRow row in tabl.select(“”,”ProductID”) …………… Dim DS new Dataset 记住此乃写代码之大忌; Dim DT new Databable…. …………. Next 还有一种写法 Dim DS new Dataset Dim DT new Databable… Foreach (DataRow row in tabl.select(“”,”ProductID”) DS=GetDatase DT=GetDatatable…………… . …………. Next
正确的写法是: Dim DS new Dataset Dim DT new Databable… Try Foreach (DataRow row in tabl.select(“”,”ProductID”) DS=nothing ‘每次使用,都先把内存空间释放出来 DT=nothing ‘每次使用,都先把内存空间释放出来 DS=GetDatase DT=GetDatatable…………… . …………. Next Catch ex As Exception Throw ex Finally DS.dispose DT.dispose End Try
另外,提醒大家一点,记得用 For Each 语句替代For i=0 to Rowcount-1,这样的效率改善也是明显的。
“請將Web.config中的debug及Trace均設為False。還有您的所有程式請確 保compile為Release Mode Application set up for debugging One reason for high memory that we see here in Support a lot is when you have debugging, tracing, or both enabled for your application. While you are developing your application, this is a necessity.By default, when you create your application in Visual Studio .NET, you will see the following attribute set in your Web.config file:
and/or
Also, when you do a final build of your application, make sure that you do this in "Release" mode, not "Debug" mode. ”
下面也是原话 Pls help to check the Run In Rack Job program . It will no response after running two or three days . the AP server Memory usage will over 2.5G . after we close the the program , Memory will decrease to 1.5 .
1. Is one application pool’s maximum memory usage 1.5G? A&: Each application pool is a w3wp.exe. w3wp.exe is a process. Every process has 2 G User mode virtual address, so the maximum memory usage for application pool is 2G. However, you can’t make sure that there is no memory fragment issue. Therefore, Out of memory always occur after 1.5 G according to our experience. 2. Is each application pool independent on memory usage? A&: Different application pools are different w3wp.exe, so each application pool’s maximum memory usage is 2G. 3. Can setup maximum CPU usage on each application pool? A&: You can monitor it, but you can’t setup it.