MySqlDataReader is used in Using, and mysqldatareader

Source: Internet
Author: User

MySqlDataReader is used in Using, and mysqldatareader

Conclusion:When the DataReader is placed in the Using method, the resources are automatically released. If an exception occurs during the process, the occupied resources are also released.
Test process: The entire process is not recorded here. I just want to give a general description of the results. If you are interested, you can test it on your own.
First, the normal processing process:

1 MySqlDataReader dr = MySqlHelper. executeReader (MySqlHelper. conn, CommandType. text, sqlStr, parameters) 2 while (dr. read () 3 {4 keyWords. add (dr ["KeyWord"]. toString (); 5} 6 dr. close ()View Code

This process seems to be okay, but in the test, if "keyWords. add (dr ["KeyWord"]. toString (); "when an exception occurs, the program will be adjusted to the exception processing module. In this way, the close method below will not be executed, as a result, the number of database connections increases constantly. When the maximum value is reached, the problem becomes apparent.

BottomMethod 1Exception Handling:

1 MySqlDataReader dr = MySqlHelper. executeReader (MySqlHelper. conn, CommandType. text, sqlStr, parameters); 2 try {3 while (dr. read () 4 {5 keyWords. add (dr ["KeyWord"]. toString (); 6} 7} 8 catch () {9} 10 finnally {11 dr. close (); 12}View Code

There is no doubt that this method is easy to think.

Method 2Here, we plan to use the using method for processing. However, based on theoretical knowledge, this is acceptable. However, the real program running environment is sometimes not guided by theoretical knowledge. Now there is a suitable testing environment for the program. Why not test it by yourself? So there is a process:
I am using a MySql database and a program written in C.
First, add someBasic knowledge:
1. Using defined range: Immediate release of resources and release of resources at the end of the range. When a class instance is used in a code segment, You can automatically call the Dispose method of the class instance to release resources for whatever reason.
When the using statement is reached or an exception is thrown in the middle and the block is removed, the Dispose method of the instance is triggered to release resources.
Then, view the implementation of MySqlDataReader:
Public sealed class MySqlDataReader: DbDataReader, IDataReader, IDisposable, IDataRecord {...}
It does inherit the IDisposable method, which should be correct in theory.
2. Check the number of connections in MySql:
Command: show processlist; if it is a root account, you can see the current connection of all users. For other common accounts, you can only view the connections you are using.
Show processlist; only the first 100 items are listed. If you want to list them all, use show full processlist;

With these two points of theoretical knowledge, the following tests are much easier:
1. Do not use using or close the connection:

1 MySqlDataReader dr = MySqlHelper. executeReader (MySqlHelper. conn, CommandType. text, sqlStr, parameters); 2 while (dr. read () 3 {4 keyWords. add (dr ["KeyWord"]. toString (); 5}View Code

Test, the number of connections continues to increase.

2. Do not use. Disable the operation:

1 MySqlDataReader dr = MySqlHelper. executeReader (MySqlHelper. conn, CommandType. text, sqlStr, parameters); 2 while (dr. read () 3 {4 keyWords. add (dr ["KeyWord"]. toString (); 5} 6 dr. close ()View Code

Test, the number of connections does not change.

3. If you do not use Using, disable the operation and create an exception during the execution process:

1 MySqlDataReader dr = MySqlHelper. executeReader (MySqlHelper. conn, CommandType. text, sqlStr, parameters); 2 while (dr. read () 3 {4 keyWords. add (dr ["Keyord"]. toString (); 5} 6 dr. close ()View Code

Test, the number of connections continues to increase.

4. Use Using. No exception occurs:

1 Using (MySqlDataReader dr = MySqlHelper. executeReader (MySqlHelper. conn, CommandType. text, sqlStr, parameters) 2 {3 while (dr. read () 4 {5 keyWords. add (dr ["KeyWord"]. toString (); 6} 7}View Code

Test, the number of connections has not increased.

5. Create an exception Using:

1 Using (MySqlDataReader dr = MySqlHelper. executeReader (MySqlHelper. conn, CommandType. text, sqlStr, parameters) 2 {3 while (dr. read () 4 {5 keyWords. add (dr ["Keyord"]. toString (); 6} 7}View Code

Test, the number of connections has not increased.

After these tests, we came to the initial conclusion.

I will not talk much about it in idle time.

Author: moonlight
Time: 2015-06-09

Related Article

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.