ASP. net SQL Execution timeout Solution

Source: Internet
Author: User

Compile Program Sometimes it takes too long to execute the SQL statement for complex queries, causing the page to fail and prompting the execution script to time out. This is an exception.

Timeout exceptions can be divided into two types: Connection timeout and execution timeout.
The former is set through sqlconnection. connectiontimeout.
The latter is set through sqlcommand. commandtimeout.

Sqlconnection. connectiontimeout
Obtain the time that is waiting before the connection is terminated and an error is generated.
The wait time (in seconds) for the connection to open ). The default value is 15 seconds.

Sqlcommand. commandtimeout
Obtain or set the wait time before terminating the Command Execution Attempt and generating an error.
The waiting time (in seconds ). The default value is 30 seconds.

 

 

This is the method found on the Internet:
Conn. open ();
Sqlcommand sqlcmd = new sqlcommand ();
Sqlcmd. commandtimeout = 180; // sqlcmd timeout is 3 minutes
You can set it as needed. If it is too long, you can also set it to 0. When this attribute is set to 0, there is no time limit. This attribute value should be used with caution. You also need to set the time limit for running HTTP requests in the web. config configuration file. 
<System. Web> 
<Httpruntime maxrequestlength = "102400" executiontimeout = "720"/>
</System. Web>
The value is set to 720 seconds. The preceding maxrequestlength attribute is generally used to limit the size of uploaded files! The default value is generally 4096kb (4 MB ).

 

another method I use in projects:
because a project needs to import at least 0.2 million records at a time for category filtering, calculation, and warehouse receiving operations.
the server environment is not very good and too many websites are mounted. Therefore, the entire conversion process may take more than half an hour, in this case, sessions on both the browser and server may time out. In addition, you do not have to wait for the task to complete. Of course, it is better to view the task progress.

solution:

1. Introduce a static object to record the current operation status, and use ajax to display the task completion progress in real time. Because a static object is used, even if the user temporarily leaves the page and enters the page again, it is still in progress;

2. Introduce background threads, operations on static objects by threads can effectively solve the timeout problem.

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.