title: Entity Framework 5 Connection Pooling In MultiThreading
body: <p>I am relatively new to Entity Framework and have limited knowledge on entity frameworks/sqlservers connection pooling functionality. I have to implement a multithreaded operation that includes database changes. </p>
  
  <p>The MainMethod looks for new database entries and queues up the tasks using ThreadPool.QueueUserWorkItem(). </p>
  
  <p>I am having database operations in the child threads too. For that I am creating the dbcontext object in the child thread.</p>
  
  <p>I would like to know if the way I have implemented will have any performance issues. Will my child threads be creating new connection strings or reusing available ones. I have read that Entity framework makes use of the sql server pooling functionality but don't know how exactly.</p>
  
  <pre><code>public class DataProcessor
  {  
      public void MainMethod(DataUow dataUow) //Entry point (Main thread)DataUow is the repository class
      {
          while (true)
          {
              try
              {
                  var toDoMessageList = dataUow.MessageDumps.GetAll().ToList();  //Get Records
                  if (toDoMessageList.Count() &gt; 0)
                  {
                      foreach (var message in toDoMessageList)
                      {
                          ThreadPool.QueueUserWorkItem(RunThread, message); 
                          message.ThreadHandlingStatus = 2; //Do changes to message 
                          dataUow.MessageDumps.Update(message); //and update           
                      }
                      dataUow.Commit();
                  }
                  else
                  {
                      Thread.Sleep(500);
                  }
              }
              catch (Exception ex)
              {
                  //Do Something
              }
          }
      }
  
      private  void RunChildThread(object messageDataSent)
      {
          using (var dataContext = new DataUow(new RepositoryProvider(new RepositoryFactories())))
          {
              try
              {
                  //Some Long Task Including Database changes
                  dataContext.Commit(); //Repository method that contains the entity framework .SaveChanges()
              }
              catch (Exception ex)
              {
                  //Do Something
              }
          }
      }
  }
  </code></pre>
  
  <p>DataUow is the repository pattern class.
  Thank you.</p>