Answered linq to sql connection open?

  • Thursday, February 07, 2013 9:57 PM
     
     

    In a C# 2008 application, I am using linq to sql statements that look like the following: 
         Case 1:              

            using (RPTDataContext rptData = new RPTDataContext())
                            {

                                var RPTupdate = (from a in rptData.Trackings
                                                  where a.Package_ID == pkgId
                                                  select a).SingleOrDefault();
                             }

           Case 2:
               TDataContext TData = new TDataContext();
             var TNumber = (from dw in cms_TData.people
                          where dw.Organization_Name.ToUpper().Trim() == strOrgnizationName.Trim()


    I am asking that question since an application I am working on is having a memory leak. I am trying to
    rule out the above statements as the reason for the memory leak.

    My question is could either of this statements cause a memory leak, leave a database connection open,
    keep the database object 'alive' in memory? If so, can you tell me what statement could cause that kind of
    problem and how to write the code so the problem does not persist?

All Replies

  • Thursday, February 07, 2013 10:04 PM
     
     Answered

    The second case doesn't dispose of the TDataContext - this should eventually clean itself up, but it'd be better to wrap it in a using statement so you don't delay the collection/cleanup.


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".