. Easy to be neglected in the process of net development

Source: Internet
Author: User
Tags empty end exception handling functions sql net oracle database connectionstrings
In using Visiol Studio.NET to develop Web applications, developers often encounter some problems: such as I developed a good program, in the development environment test is no problem, how to move to the application environment, there will be problems?

In using Visiol Studio.NET to develop Web applications, developers often encounter some problems: such as I developed a good program, in the development environment test is no problem, how to move to the application environment, there will be problems? Not the program's inability to run, is the program's efficiency slow with the snail in the crawl, this situation in. NET's new hands are particularly common. I don't know why, some introductions. NET development of the book cited in the example code, but also to ignore this problem, especially let me depressed is some of my favorite books, such as: <<ado. NET Technology Insider >>,<<asp. NET2.0 Advanced Programming (4th Edition) >> both are published by Tsinghua University Press, and there is a book worse <<visual Basic.NET professional project development >> I suggest that we should not look at it, avoid wasting time and energy.

This article is not only for. NET Developer's Novice has the help, similarly to which has the experience, also brings some enlightenment and the reference.

What kind of problems they will encounter, I may summarize to you:
1. Database connection Timeout
2. Create objects to use, regardless of release
3. Debug (Debug) mode is compiled and used in the application environment.
4. Sharing the actual mode of operation
The problem above is like a cancer that accumulates to a certain extent and has far-reaching consequences.

One, the database connection timeout article

To know the database connection timeout problem, first look at the following 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) ' Here Open, behind see 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

A partial explanation of the above line of code:
0006: A class referencing a 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 doesn't seem like a big deal, it's just a function; The database opens the connection and does not close does not affect the entire application; is that so?

Let's talk about database connectivity, in the Oracle database, the general default number of database connections is up to 100 Torai, not more than 200, even if you change the number of connections, but no matter how the number of connections is limited, it is not unlimited for you to consume.

In the Web program, not only does it not automatically shut down the database connection, a function like this will reuse a database connection every time it is invoked; if there are many functions like this, you will be waiting for an error warning page to bounce out, such as database Connection Timeout .... and other information.

That's not much, and what's more, write the following code in a circular statement like this:

[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, someone asked, I am in the development environment to test a little problem is not ah? Yes, you are no problem, I would like to ask, you develop the environment test data have a few pens?

Now, the question already knows where, how to solve?

For [sample-01] do the following, 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: Here is Open, the back does not see 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 Comment: see clsoradb.close behind
0023 return Clsresult
0024 Catch ex as Exception
0099 Clsoradb.close Comment: 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 lines and 0099 rows.

In exception handling, special reminders two points:

One, your database should be closed at the time of code line 0028, not after;

Second, some people are not used to (or inadvertently) add 0088 lines of code;

for [Sample-02] and [sample-03], the Open database connection is written 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 commit with a using statement. NET application garbage collector collection automatically; There are a lot of articles about it, and there are no special details here.

Second, the object just create applications, regardless of the release of the article

We continue with the [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 been released.

The code explanation for the "0004" line is to dtresult a space in memory to the defined object; How much space does the system divide? Ah, I have not studied (for those who are willing to do, hehe ...). But one thing, to partition a space in memory, is to occupy memory. Then how much memory, not infinite bar, is also limited, all the results of running the above code, the final result is that the efficiency of the system more and more slowly, some people suspect that I have memory 1 to 2G, plus virtual memory is even greater, I can only say that your suspicion is correct. But does your application use one of these functions? I don't think so, so the application of hundreds function can be imagined in memory consumption. If it's a program that runs in the background, it's a function in time, and it crashes the system. This is just a simple example of a more complex. Object applications like this are: Dataset, Datatable,datareader,dataadapter,datagrid. Wait

So how do we 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 following statements are released

Dim Dtresult as New DataTable
Dim DR as New DataReader
Dim DS as New Dataset
Try
..
.................
Catch ex as Exception
--Releasing the Applied object
Throw EX
Finally
--After use, release the applied object
Dtresult.dispose--Clear the object from memory
Dr.dispose-Clear the object from memory
Ds.dispose-Clear the object from memory
End Try

Some people used to write the following:
Dim Dtresult as New DataTable
Dim DR as New DataReader
Dim DS as New Dataset
Try
..
' After use, release the applied object
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 Applied object
Throw EX
Finally
End Try


This is not also released? What I want to ask is, if the program appears to be abnormal, will they be released? I must tell you that they must not be released, and in order to ensure the stability of the program, I recommend that you use a try Catch statement.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.