简体   繁体   中英

using multiple transactions in single connection

Basically it's a patching mechanism

Here is what I'm doing :

  1. Open a SQL connection.
  2. Begin the transaction.
  3. Update a record in database for the version of the software.
  4. Execute some more queries on same database by using same connection.
  5. Download a 15 to 20 MB file.
  6. Execute a select query by using the same connection.
  7. Commit the transaction.
  8. Close the transaction.

This sequence is causing the problem of SQL Connection timeout as it takes time to download the file. The problem is that I can commit the transaction only after downloading the file and not before that.

Writting the code in C# . Database used is SQLCE Here is some part of the code:

SqlCeConnection conn = new SqlCeConnection("ConnectionString");

conn.Open();
SqlCeTransaction ts = conn.BeginTransaction();

//A method call executes all the methods that with parameters
(string sqlQuery, ref SqlCeConnection conn, SqlCeTransaction ts)
{
SqlCeCommand cmd = new SqlCeCommand();
cmd.Connection = conn;
cmd.Transaction = ts;
cmd.CommandText = sqlQuery;
cmd.ExecuteNonQuery();
}

//A method call downloads the file of 15 to 20 MB

//A method executes a select query that returns the version of the software by using same SQL connection.

//The above query gives the error of SQl connection timeout 
ts.Commit();
conn.Close();

Can any one help me to solve the problem

This is what I did.

I passed the same connection and transaction object to the method which was downloading the file. In that method I executed a simple select query in the loop where the file was getting downloaded. This helped to keep the connection and transaction active. Here even if your internet connection speed is slow it does not affect as in each loop your SQl query gets fired and keep the connection active.

Set you command timeout manually.

cmd.CommandTimeout = 180;

This example sets the Timeout to 180 seconds (3 minutes)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM