开发者

Transaction Timeout exception in a console app running series containing series of SPs

开发者 https://www.devze.com 2023-04-12 01:32 出处:网络
I have got a console application in which I have couple of stored procedures called inside the transaction scope which looks as below.

I have got a console application in which I have couple of stored procedures called inside the transaction scope which looks as below.

 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();
                    }
                }
                c开发者_Go百科atch (Exception ex)
                {
                    throw new Exception(String.Format("Method - Common.CallSPMethod1: {0}", ex.Message), ex);
                }
            }

After running for about 20-30 minutes, the call to frist SP completes, but throws an exception like:

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()

Can anyone help me how to get over this problem?


I always refrain from using transaction scope as it acts funny at times

try using sqltranssaction . You code should look similar to this

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

精彩评论

暂无评论...
验证码 换一张
取 消