TransactionScope Prematurely Completed

前端 未结 7 854
[愿得一人]
[愿得一人] 2020-12-13 02:01

I have a block of code that runs within a TransactionScope and within this block of code I make several calls to the DB. Selects, Updates, Creates, and Deletes, the whole ga

7条回答
  •  独厮守ぢ
    2020-12-13 02:31

    I can reproduce the problem. It is a transaction timeout.

    using (new TransactionScope(TransactionScopeOption.Required, new TimeSpan(0, 0, 0, 1)))
    {
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            connection.Open();
            using (var sqlCommand = connection.CreateCommand())
            {
                for (int i = 0; i < 10000; i++)
                {
                    sqlCommand.CommandText = "select * from actor";
                    using (var sqlDataReader = sqlCommand.ExecuteReader())
                    {
                        while (sqlDataReader.Read())
                        {
                        }
                    }
                }
            }
        }
    }
    

    Throws System.InvalidOperationException with this message:

    The transaction associated with the current connection has completed but has not been disposed. The transaction must be disposed before the connection can be used to execute SQL statements.

    To solve the problem make your query run faster or increase the timeout.

提交回复
热议问题