运行包含一系列SP的系列的控制台应用程序中的事务超时异常

时间:2011-10-11 15:27:18

标签: c# .net sql-server sql-server-2008 transactionscope

我有一个控制台应用程序,其中我在事务范围内调用了几个存储过程,如下所示。

 using (TransactionScope scope = new TransactionScope(new TransactionScopeOption(), new TimeSpan(8,0,0)))
                        {      
                            CallSPMethod1();
                            CallSPMethod2();
                            CallSPMethod3();

                            //Commit all steps in transaction scope
                            scope.Complete();
                            transactionStatus = 1;
                        }
    public static void CallSPMethod1()
            {
                try
                {
                    using (SqlConnection conn = new SqlConnection(Messages.connectionString))
                    {
                        conn.Open();

                        var sqlCommand = new SqlCommand("CallSPMethod1", conn);

                        // Set the command type that you will run.
                        sqlCommand.CommandType = System.Data.CommandType.StoredProcedure;

                        sqlCommand.Parameters.Add("@i_Execution_Batch_ID", SqlDbType.UniqueIdentifier, 16);
                        sqlCommand.Parameters[0].Value = Messages.BatchId;

                        sqlCommand.CommandTimeout = 3600;
                        // Run the SP
                        sqlCommand.ExecuteNonQuery();
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception(String.Format("Method - Common.CallSPMethod1: {0}", ex.Message), ex);
                }
            }

运行约20-30分钟后,对frist SP的调用完成,但抛出异常,如:

System.Transactions.TransactionException: The operation is not
valid for the state of the transaction. ---> System.TimeoutException: Transactio
n Timeout
   --- End of inner exception stack trace ---
   at System.Transactions.TransactionState.EnlistPromotableSinglePhase(InternalT
ransaction tx, IPromotableSinglePhaseNotification promotableSinglePhaseNotificat
ion, Transaction atomicTransaction)
   at System.Transactions.Transaction.EnlistPromotableSinglePhase(IPromotableSin
glePhaseNotification promotableSinglePhaseNotification)
   at System.Data.SqlClient.SqlInternalConnection.EnlistNonNull(Transaction tx)
   at System.Data.SqlClient.SqlInternalConnection.Enlist(Transaction tx)
   at System.Data.SqlClient.SqlInternalConnectionTds.Activate(Transaction transa
ction)
   at System.Data.ProviderBase.DbConnectionInternal.ActivateConnection(Transacti
on transaction)
   at System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection ownin
gObject)
   at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection ow
ningConnection)
   at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection ou
terConnection, DbConnectionFactory connectionFactory)
   at System.Data.SqlClient.SqlConnection.Open()

任何人都可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

我总是不使用交易范围,因为它有时很有趣

尝试使用sqltranssaction。您的代码应该与此类似

sqlconnection con=new sqlconnection(constr);
sqltransaction tran=con.begintransaction();
                       try {      
                            CallSPMethod1(tran);
                            CallSPMethod2(tran);
                            CallSPMethod3(tran);



                            //Commit all steps in transaction scope
                           tran.commit
                            transactionStatus = 1;
                        }
                        catch{
                         tran.rollback();
                              }
    }

    public static void CallSPMethod1(sqltransaction tr)
            {
                try
                {
                    sqlconnection con=tr.connection;
                    {
                     //do ur work here
                        }
                 }
                 catch{}
              }