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

本文关键字:事务 超时 异常 应用程序 控制台 包含 一系列 sp 运行 | 更新日期: 2023-09-27 18:11:01

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

 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分钟后,对第一个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()
谁能帮我解决这个问题?

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

我总是避免使用事务作用域,因为它有时会很有趣

尝试使用sqltransaction。您的代码应该类似于

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{}
              }