Info regarding multithreaded sql statements

2 posts, 0 answers
  1. Norbert
    Norbert avatar
    35 posts
    Member since:
    Dec 2009

    Posted 02 Dec 2011 Link to this post

    Hello,

    I want to exec sql statements in multithreaded environment. When you look for the following example - why are there several Sql Statements like SET LOCK_TIMEOUT etc?
    internal class Program
        {
     
            public static int i=0;
            public static Object lockvar = ""; // Lock-Variable
     
            private static void Main(string[] args)
            {
                CheckTime("Initialize Application");
                AppInitializer.InitializeApp();
                 
                Task t1 = new Task(DoIt);
                Task t2 = new Task(DoIt);
                Task t3 = new Task(DoIt);
                t1.Start();
                t2.Start();
                t3.Start();
     
                Task.WaitAll(t1, t2, t3);
                Console.WriteLine("The end");
                Console.ReadLine();
            }
     
            public static void DoIt()
            {
                Thread.Sleep(2000); //simulate longer running task...
                lock ( lockvar )
                {
                    string mystring = "Thread started... ";
                    i++;
                    mystring += i;
                    CheckTime(mystring);
                    using ( IRepository<SysProperty> rep = App.Instance.GetService<IRepository<SysProperty>>() )
                    {
                        rep.AutoCommit = false;
                        var o = rep.AdoGetSql("Select * From dbo.tblSysProperty where name = 'DbVersion'");
                        CheckTime(o.First().Name.ToString());
                        o.First().Value = "Testwert " + i.ToString();
                        rep.SaveChanges();
                        Console.WriteLine(rep.LogOutput);
                    }
                    //Repository.SaveChanges();
                    System.Console.WriteLine("Alles gespeichert...{0}", i.ToString());
                }
            }
     
            private static void CheckTime(string Info)
            {
                Console.WriteLine("{0} | {1}", DateTime.Now.ToString("u"), Info.ToString());
            }
        }

    How does OA handle that?
    FYI: I am using one context (in this case a repository around the OA context) per thread (task in TPL)

    Thank you in advance,
    Norbert
  2. Jan Blessenohl
    Admin
    Jan Blessenohl avatar
    707 posts

    Posted 05 Dec 2011 Link to this post

    Hello Norbert,
    We are setting the lock timeout once we open a connection. Because the connection is pooled, this extra call happens only one in it's lifetime. There are some more statements executed when you use your first context. If you use our high-low key generator, we check if the content of the extra table looks good.

    All the best,
    Jan Blessenohl
    the Telerik team

    Q3’11 of Telerik OpenAccess ORM is available for download. Register for the What's New in Data Tools webinar to see what's new and get a chance to WIN A FREE LICENSE!

  3. DevCraft banner
Back to Top