- Database Connection Timeout Chapter
To know the database connection time-out problem, look at the following section of code:
[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) ' After Open here, there is no clsoradb.close at the back
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
A partial explanation of the above line of code:
0006: Class referencing the database connection;
0008: Open the database connection;
Then, the entire function you can not find the action to close the database connection, is to wait for the operating system to release it? Some people say, it seems that there is no big deal, this is just a function; Database open connection, not closed does not affect the entire application; Is that true?
Let's talk about the database connection problem, in the Oracle database, the general default database connection number is 100 Torai, no more than 200, even if you change the number of connections, but anyway, its number of connections is limited, can not be unlimited for you to consume.
In the Web program, it not only does not automatically shut down the database connection, like such a function will be called every time, will be re-use a database connection; If there are a lot of functions like this, you just wait for a bug warning page to pop up, such as database Connection Timeout ... and other messages.
This is nothing, and what's more, altogether writes the following code in the loop statement, such as:
[SAMPLE-02]
Foreach (DataRow row in Tabl.select ("", "ProductID")
...............
Clsoradb.open (strconn)
.............
Next
Some people also like to play the following statement:
[SAMPLE-03]
Foreach (DataRow row in Tabl.select ("", "ProductID")
Foreach (DataColumn col in Tbl.columns)
...............
Clsoradb.open (strconn)
Next
.............
Next
Speaking of which, some people ask, I in the development environment test a little problem is not it? Yes, you are no problem, I want to ask is, your development environment test data how many?
Now, the problem already knows where, how to solve?
For [sample-01] do the following processing, note the following code:
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) Note: After this Open, there is no clsoradb.close at the back
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 Note: See Clsoradb.close later
0023 Return Clsresult
0024 Catch ex as Exception
0099 Clsoradb.close Note: Program exception also see Clsoradb.close
0025 Clsresult.resultid =-1
0026 Return Clsresult
0028 Throw EX
0027 End Try
End Function
Note the above two lines of code: 0088 rows and 0099 lines.
During exception handling, special reminders are two points:
One, your database should be closed at the time of the code line 0028 ago, not after;
Second, some people are not accustomed (or negligent) add 0088 lines of code;
for [Sample-02] and [sample-03], write the Open database connection before all the loop statements, such as:
Clsoradb.open (strconn)
Foreach (DataRow row in Tabl.select ("", "ProductID")
...............
.............
Next
There is, of course, another way to use the using language
- object to create an app, regardless of the release
We continue to use [Sample-01] code, we now look at 0004 lines of code:
0004 Dim Dtresult as New DataTable
Who will find it released, you can't, I can't, never have been released.
The code interpretation of the "0004" line is to divide a space in memory to give this defined object Dtresult; How much space does the system have to divide? Yes, I have not studied (left to those who have a good people, hehe ...). But one thing is to partition a space in memory, that is, to occupy memory. Then how much memory, not infinite bar, but also limited, all running the above code the end result is that the system's execution efficiency is more and more slow, some people suspect that I have 1 to 2G of memory, plus virtual memory is even bigger, I can only say that your suspicion is correct. But does your application use such a function? I guess not, so the application of the hundreds of functions can imagine the memory consumption. If it is a program that runs automatically in the background, it is a function in time, which can also cause the system to crash. This is just a simple example, with more complexity. Objects like this are also used: Datasets, Datatable,datareader,dataadapter,datagrid. such as
So how to solve these problems:
2.1 Define the object used before the try catch statement, such as:
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 The statements released are as follows
Dim Dtresult as New DataTable
Dim DR as New DataReader
Dim DS as New Dataset
Try
..
.................
Catch ex as Exception
--Release the app's objects
Throw EX
Finally
--When you're done, release the app's objects
Dtresult.dispose--Clear the object from memory
Dr.dispose-Clear the object from memory
Ds.dispose-Clear the object from memory
End Try
Someone used to write the following:
Dim Dtresult as New DataTable
Dim DR as New DataReader
Dim DS as New Dataset
Try
..
' When you're done, release the app's objects
Dtresult.dispose ' Clear the object from memory
Dr.dispose ' Clear the object from memory
Ds.dispose ' Clear the object from memory
Catch ex as Exception
' Release the app's objects
Throw EX
Finally
End Try
It's not released, is it? What I want to ask is, if the program is abnormal, will they be released? I must tell you that they must not be released, in order to ensure the stable operation of the program, I would recommend that you use the Try Catch statement.
2.3 Never recommend writing the following statement in the Loop statement, specifically why, think for yourself.
Foreach (DataRow row in Tabl.select ("", "ProductID")
...............
The Dim DS new Dataset remembers this as a great taboo for writing code;
Dim DT New databable ....
.............
Next
There's another way.
Dim DS New Dataset
Dim DT New databable ...
Foreach (DataRow row in Tabl.select ("", "ProductID")
Ds=getdatase
Dt=getdatatable .........
.
.............
Next
The correct wording is:
Dim DS New Dataset
Dim DT New databable ...
Try
Foreach (DataRow row in Tabl.select ("", "ProductID")
Ds=nothing ' Every use, first release the memory space
Dt=nothing ' Every use, first release the memory space
Ds=getdatase
Dt=getdatatable .........
.
.............
Next
Catch ex as Exception
Throw EX
Finally
Ds.dispose
Dt.dispose
End Try
In addition, it is important to remind you that it is also obvious to use the for each statement instead of the for i=0 to Rowcount-1.
- debugging (Debug) mode compilation is used for the application environment medium
Let's look at the following picture:
Will anyone pay attention to this interface? There is, but certainly not much.
Next, the program is developed (and also includes unit tests) and then compiled directly to the application environment.
The whole process is over. No one thought, buried a deep mine here, according to the people of Microsoft, the distribution of the program to the application environment, how much memory you are probably not enough.
So Microsoft suggested that we do the following:
"Please set debug and trace in Web. config as false." And all your programs, please ensure that compile is the release Mode
Application Set up for debugging
One reason for high memory so we see the here in support a lot are when you had debugging, tracing, or both enabled for your Application.
While you are developing your application, this is a necessity. By default if you create your application in Visual Studio. NET, you'll see the following attribute set in your WEB.C Onfig file:
<compilation debug= "true"/>
and/or
<trace enabled= "true" .../>
Also, when you do a final build of your application, make sure, the "Release" mode, not "Debug" mode. ”
What happens if we don't do this? I share a colleague's feelings to you:
"Already is very formidable DB and the AP server (all is the blade server), but the question again to happen again and again, that feeling really is helpless very desolate ah ~"
Later, it was found that the memory usage is high, to a certain extent, it will slow down, this time as long as the restart of IIS can be a good time. Later analysis of IIS used to entity plus virtual memory more than 2G will be blown off.
This is the reason, do you want to meet? Then try it.
- The actual operation share the article
The above three links, any one occurrence problem, will affect the system efficiency. I share some of the things that have happened in the course of our actual homework, and how to solve these problems.
4.1 Memory usage peaks, causing the program to not continue running.
One of my colleagues shared their experience as follows (the exact words to share):
We have some programs that are run by servers and have more and more potential. While writing the program, you may be less concerned about the consumption of memory.
The following example may give us a little revelation.
Here's the exact words.
Pls Help to check the Run in Rack Job program. It'll no response after running, or three days. The AP server Memory usage would over 2.5G. After we close the program, Memory would decrease to 1.5.
The general meaning is: in the server side (also called backstage) automatically run a program, after running for two or three days, stopped running. When checking the memory usage of the server, it was found to be over 2.5G, and after shutting down the program, it dropped to 1.5G ... The picture below is for proof:
4.2 Process requests too much, causing the CPU to not be processed in time, the program efficiency response is slow.
Here are my colleague's words:
"After a year, the production is increasing and new problems arise." From the server performance analysis, and the last memory is too high is the CPU utilization.
Whenever the CPU is too high, the production line will respond to a large area of slow response (which is related to which AP to connect to).
Each time we were slow, we found the Ap,recycle IIS application pool that was too high for the CPU to be OK.
So I again find Bon help Analysis (Conclusion: Microsoft closed report 20090226v1-srt090119833891 Web service can ' t serve IISReset can fix.msg). Some suggestions for developing the program are also given.
The conclusion is that no process takes up a particularly high CPU, and no process takes too long CPU time. Just a request to the DB process too much (compared to the actual situation of 3 factory-more accessories, brush faster), add up on the overall too high.
Also found a lot of DLLs are built in debug mode, these DLLs occupy too much memory resources.
Later, according to Bon's proposal, we modified the IIS application pool settings as follows, to resolve too many requests can not be processed in a timely manner, resulting in high CPU problems. ”
Here are some questions and answers about the settings for the Application connection pool (application pool), which is helpful for understanding this setting:
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 do sure that there is no memory fragment issue. Therefore, out of the memory always occur after 1.5 G according to our experience.
2. Are each application the pool Independent on memory usage?
A&: Different application Pools is 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 and you can ' t setup it.
Asp. NET development Web site program Common error Summary