none
How to lock a submitchanges() process of datacontext in multithread, Please help me! RRS feed

  • Question

  • I build a project base on ASP.NET MVC use LINQ to SQL to query database.

    The requirement of project is the system must submit datacontext to database and Get ListObject to Appliction level every 5 minutes.

    So i create a timer in Global.asax file.

    My code is:

     protected System.Timers.Timer timer1 = new System.Timers.Timer();

     FieldDetailRepository fieldDtRepository = new FieldDetailRepository();  
             

            protected void Application_Start()
            {
                       
                         
                timer1.Interval = 300000;
                timer1.Elapsed += new System.Timers.ElapsedEventHandler(timer1_Elapsed);
                timer1.Start();
                timer1.Enabled = true;
               
                                     
               
            }

     private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
            {        
                    
                listFieldDetail = fieldDtRepository.GetListFieldDetail();
                Application.Lock();        
                Application["ListFieldDetail"] = listFieldDetail;
                Application.UnLock();
               
            }

    In FieldDetailRepository class i use a datacontext object for all functions  include GetListFieldDetail().

     public class FieldDetailRepository
        {
            private TestDataContext db = new TestDataContext();

           .......

        }

     

    In another part of project, I create a view to submit somethings change to  database

      public static void Update(EditableAdODetail AdODetail)
            {
                try
                {
                    TestDataContext db = new TestDataContext();
                   
                           ........
                          
                    db.SubmitChanges(ConflictMode.ContinueOnConflict);
                                           
                }
                catch (ChangeConflictException)
                {
                    throw;
                }
            }

    No error occur when i submit from the view. But sometime the submitchanges() method not effective.

    I realized that something wrong from timer because it is a thread.

    Who can help me solve this problem.

    Thanks

    Tuesday, September 28, 2010 5:05 AM

Answers

  • To day, i found solution for my problem.

    Just do that in my code:

     timer = (System.Timers.Timer)HttpContext.Application["Timer"]; 

                    timer.Stop();

                    SessionAdODetailRepository.Update(adDetail);

                    timer.Start();

    when i stop timer, no conflicting between timer thread and main thread. So everything allright.

    After 5 minutes, my webapp submit data 1 time. It runs well.

    Sorry for my bad EL.

    Thanks for reading :D

    Wednesday, September 29, 2010 4:44 PM

All replies

  • Ah, i realized that everytime a datacontext is created in timer_Elapsed event, all other datacontexts will be detroyed. So Submitchange() method will not effect during the time timer_elapsed event occur.

    So i need to lock and keep datacontext in controller mantain while timer_Elapsed event invoked.

    Tuesday, September 28, 2010 6:21 AM
  • To day, i found solution for my problem.

    Just do that in my code:

     timer = (System.Timers.Timer)HttpContext.Application["Timer"]; 

                    timer.Stop();

                    SessionAdODetailRepository.Update(adDetail);

                    timer.Start();

    when i stop timer, no conflicting between timer thread and main thread. So everything allright.

    After 5 minutes, my webapp submit data 1 time. It runs well.

    Sorry for my bad EL.

    Thanks for reading :D

    Wednesday, September 29, 2010 4:44 PM