Connection Pool leaked ~

Source: Internet
Author: User

I have been working on this project for nearly three months. I always feel slow when I test the webpage, but I have never found any problems. In a complex page created a few days ago, a button calls the data access layer method several times. After several operations, the page will collapse. The returned error is roughly: the connection pool has reached the maximum limit and cannot open a new connection. The connection pool leaked.

The methods I wrote in the database access layer call the methods provided in the SQLHelpe class of MS to directly access the database. The ExecuteReader method is as follows:

Public static SqlDataReader ExecuteReader (string connString, CommandType primitive type, string plain text, params SqlParameter [] parallel parms)
{
SqlCommand cmd = new SqlCommand ();
SqlConnection conn = new SqlConnection (connString );

// We use a try/catch here because if the method throws an exception we want
// Close the connection throw code, because no datareader will exist, hence
// CommandBehaviour. CloseConnection will not work
Try
{
PrepareCommand (cmd, conn, null, partition type, plain text, plain parms );
SqlDataReader rdr = cmd. ExecuteReader (CommandBehavior. CloseConnection );
Cmd. Parameters. Clear ();
Return rdr;
}
Catch
{
Conn. Close ();
Throw;
}
}

CommandBehavior. closeConnection. I want to automatically close the data connection after reading the data connection. I didn't close it manually in my code. Instead, it turned out to be disabled automatically. InWilliam VaughnmIn his article, he talked about the possibility of connection pool leakage:

"After the code completes DataReader, You need to disable SqlConnection before the SqlConnection object stops working. To process a row set, you can pass the DataReader to another routine in the application, but you still need to ensure that the DataReader and its connection are closed. If you do not close SqlConnection, the code will "leak" the connection for each operation, so the connection pool accumulates the connection and eventually overflows. Unlike in ADO and Visual Basic (VB) 6.0, the. NET garbage collector does not close SqlConnection and clean it for you.

"You may also encounter problems when using the DataAdapter object. The DataAdapter Fill and Update Methods automatically enable the connection of the DataAdapter object and disable the connection after the data I/O operation is complete. However, if the connection is enabled when the Fill or Update method is executed, ADO. NET will not close SqlConnection after the method is executed. This is another opportunity for connection "leakage ."

Since CommandBehavior. CloseConnection cannot be closed after the connection is used, what is the purpose of CommandBehavior?

"This option works only when you use complex binding controls in ASP. NET Web applications. Loop the entire DataReader result set to the end of its row set (that is, when Dr. Read-DataReader's Read method-returns False) is not enough to trigger the connection to automatically close. However, if you bind to a complex binding control (for example, DataGrid), the control will close DataReader and Connection"

After each use of the DataReader object returned by SQLHelper, add a statement to manually close the Reader. after each use of SQLHelper's database update operation, add the statement to close the database connection. The problem is solved.

 

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.