locked
Avoiding Timeout LINQ query's RRS feed

  • Question

  • I have been trying to understand connection timeouts for the LINQ to SQL, but I am not making any progress. For example, I had some code with five or six "JOIN" clauses, but the query times out in a little less than three minutes, so I tried to recreate the problem with a vary simple but basically the same example.  The only difference is: there are no "JOIN" clauses; I have an delay timer within my simpler example; and I'm using a Console Application instead of a WPF Application.  The only problem is this simpler version wouldn't timeout.  I stopped it after 30 plus minutes.  Can anyone explain what is going on?

    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace Connection_Time
    {
        class Program
        {
            static void Main(string[] args)
            {
                DateTime current = DateTime.Now;
                Console.WriteLine("Starting Time:  " + current.ToShortTimeString());
    
                ProccessingData();
                DateTime set = DateTime.Now;
                Console.WriteLine("Finished Time:  " + set.ToShortTimeString());
                Console.WriteLine("Time Spent:  " + (set.Minute - current.Minute).ToString() + "min "
                    + (set.Second - current.Second).ToString() + "s " + (set.Millisecond - current.Millisecond).ToString() + "ms");
            }
    
            private static void ProccessingData()
            {
                DataClasses1DataContext DB = new DataClasses1DataContext();
    
                try
                {
                    var query = from part in DB.Parts
                                select part;
    
                    Console.WriteLine("There are " + query.Count().ToString() + " parts.");
                    int count = 0;
                    foreach (var q in query)
                    {
                        Console.WriteLine(count.ToString() + " " +DateTime.Now.ToLongTimeString() + "  " + q.AFLPartNumber);
                        Delay();
                        count++;
                    }
                }
                catch
                {
                }
            }
    
            private static void Delay()
            {
                DateTime current = DateTime.Now;
                DateTime set = current.AddMilliseconds(500);
    
                while (set >= current)
                {
                    current = DateTime.Now;
                }
            }
        }
    }
    

    Wednesday, July 9, 2014 5:31 PM

Answers

  • The SQL COmmand has a timeout that I often have to increase with queries that return a lot of data.  Below is an example of the command.

    dataAdapter.SelectCommand.CommandTimeout


    jdweng

    • Marked as answer by Anne Jing Thursday, July 17, 2014 6:41 AM
    Wednesday, July 9, 2014 5:51 PM

All replies

  • Hello,

    Are you indicating that the query with or without joins take 30 minutes plus to run ? Perhaps the issue is with the actual query and not the code per-say.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    Wednesday, July 9, 2014 5:35 PM
  • The SQL COmmand has a timeout that I often have to increase with queries that return a lot of data.  Below is an example of the command.

    dataAdapter.SelectCommand.CommandTimeout


    jdweng

    • Marked as answer by Anne Jing Thursday, July 17, 2014 6:41 AM
    Wednesday, July 9, 2014 5:51 PM
  • Let's say you would increase the timeout span, this still means you have an app with low performance. Try replacing the Linq with an Sql query, i think it might run faster that the generated code by Linq 
    Thursday, July 10, 2014 8:24 AM
  • Do you achieve the timeout after removing the line that calls query.Count()?

    Thursday, July 10, 2014 9:02 AM
  • It seems like if there are no joins it would run 30 minutes plus.  Sorry for not respond so soon.
    Thursday, July 24, 2014 2:14 PM
  • The code given will run 30 plus minutes while continuously pulling data from a remote database, not a local database.
    Thursday, July 24, 2014 2:16 PM
  • Surprisingly, I never had to increase the timeout for this example.  It just happens to last forever.
    Thursday, July 24, 2014 2:18 PM