Troubleshoot problems with sqltransaction exhaustion (SQL processing timeout)

Source: Internet
Author: User
Tags commit rollback access database
Sometimes the amount of data processed by the program is relatively small, well-organized, all safe and sound, but the amount of data, the original latent problem is exposed.
The code for the original Access database is:
1SqlConnection conn = new SqlConnection (strconn);
2conn. Open ();
3SqlTransaction trans = conn. BeginTransaction ();
4try
5{
6 cengine.executenonquery (Trans,commandtype.text,sql);
7 Trans.commit ();
8}
9catch (SqlException ex)
10{
One by one trans. Rollback ();
ErrorCode = ex. number;
info = "Data operation failed:" Ex. message;
14}
15finally
16{
Trans. Dispose ();
Conn. Close ();
19}
20
21st
22
At run time, the system prompts for an error when the amount of data is too large or the processing time is longer. The error message is "SqlTransaction has been used up; it is no longer available." ”
At first, I suspected it was about memory. Because the system needs to be ready for transaction rollback, every execution of a inserted or modified SQL, must have a certain cost, data volume a large, I am afraid it will be too much. But I checked the SQL Server data, and I don't see any memory issues.
It was later thought that there was a time problem with the database connection sqltransaction. The default is 15 seconds. When the volume of data is large, this time is probably not enough. Then read:
1SqlConnection conn = new SqlConnection (strconn);
2conn. Open ();
3SqlTransaction trans = conn. BeginTransaction ();
4try
5{
6 SqlCommand cmd = new SqlCommand ();
7 Cmd.commandtype = CommandType.Text;
8//Connection time changed to 300 seconds
9 cmd.commandtimeout = 300;
Ten cmd.commandtext = SQL;
One by one cmd. Connection = conn;
CMD. Transaction = trans;
CMD. ExecuteNonQuery ();
Trans.commit ();
15}
16catch (SqlException ex)
17{
Trans. Rollback ();
ErrorCode = ex. number;
info = "Data operation failed:" Ex. message;
21}
22finally
039
Trans. Dispose ();
Conn. Close ();
26}
After the modification in the test, the problem 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.