Click here to Skip to main content
15,920,503 members
Home / Discussions / C#
   

C#

 
GeneralRe: Memory leak trouble with Linq to Sql and multiple threads Pin
JD8610-Jan-16 16:41
JD8610-Jan-16 16:41 
GeneralRe: Memory leak trouble with Linq to Sql and multiple threads Pin
Dave Kreskowiak10-Jan-16 17:19
mveDave Kreskowiak10-Jan-16 17:19 
AnswerRe: Memory leak trouble with Linq to Sql and multiple threads Pin
Luc Pattyn9-Jan-16 18:07
sitebuilderLuc Pattyn9-Jan-16 18:07 
GeneralRe: Memory leak trouble with Linq to Sql and multiple threads Pin
Dave Kreskowiak9-Jan-16 18:20
mveDave Kreskowiak9-Jan-16 18:20 
GeneralRe: Memory leak trouble with Linq to Sql and multiple threads Pin
Luc Pattyn9-Jan-16 18:29
sitebuilderLuc Pattyn9-Jan-16 18:29 
GeneralRe: Memory leak trouble with Linq to Sql and multiple threads Pin
Dave Kreskowiak9-Jan-16 18:36
mveDave Kreskowiak9-Jan-16 18:36 
GeneralRe: Memory leak trouble with Linq to Sql and multiple threads Pin
JD8610-Jan-16 16:31
JD8610-Jan-16 16:31 
GeneralRe: Memory leak trouble with Linq to Sql and multiple threads Pin
Luc Pattyn10-Jan-16 17:00
sitebuilderLuc Pattyn10-Jan-16 17:00 
Quote:
I'm disposing of "db" which should close the connection shouldn't it?


I don't know as I'm not familiar with CloudPanelDbContext.

And I wouldn't rely on it unless (a) it is stated explicitly in trustworthy documentation and (b) I put in a comment repeating such fact.

Quote:
I can try using but ...


Yes you can nest try/catch/finally blocks, however it doesn't look good and most often there is no need to do so.

Actually you often don't need an explicit finally block if all disposable objects got created with using constructs, that is the beauty of it: it is hard to get it wrong, as the code is much simpler (the compiler automatically translates it to something similar to what you have, but readability is much better, so error probability goes down).

Your method, without corrections for the possible connection issues, would be simplified to:

C#
public void Execute(IJobExecutionContext context) {
    int processedCount = 0;
    try {
        using (CloudPanelDbContext db = new CloudPanelDbContext (Config.ServiceSettings.SqlConnectionString)) {
            db.Connection.Open();
            using (ExchActions powershell = new ExchActions()) {
                var mailboxDatabases = powershell.Get_MailboxDatabaseSizes();
                db.StatMailboxDatabaseSizes.InsertAllOnSubmit(mailboxDatabases);
                db.SubmitChanges();
                processedCount = mailboxDatabases.Count;
                mailboxDatabases = null;
            }
        }
    } catch (Exception exc) {
        logger.ErrorFormat("Failed to ...");
    }
    logger.InfoFormat("Processed a total of {0} mailbox databases", processedCount);
}


I'm also not familiar with Entity Framework, however assuming your CloudPanelDbContext is derived from System.Data.Entity.DbContext a quick google session told me such context knows how to deal with open connections; it seems you can open a connection and keep it associated with your context, see e.g. some of the constructors here[^].

May I suggest you read up on that topic.

PS: the same webpage also describes Dispose() with "The connection to the database ( DbConnection object) is also disposed if it was created is by this context or ownership was passed to this context when this context was created." I don't think your situation satisfies those conditions.

My intuition tells me you're nearing the solution!

Smile | :)
Luc Pattyn [My Articles] Nil Volentibus Arduum

GeneralRe: Memory leak trouble with Linq to Sql and multiple threads Pin
JD8610-Jan-16 17:06
JD8610-Jan-16 17:06 
GeneralRe: Memory leak trouble with Linq to Sql and multiple threads Pin
Luc Pattyn11-Jan-16 2:31
sitebuilderLuc Pattyn11-Jan-16 2:31 
GeneralRe: Memory leak trouble with Linq to Sql and multiple threads Pin
Richard MacCutchan10-Jan-16 20:58
mveRichard MacCutchan10-Jan-16 20:58 
GeneralRe: Memory leak trouble with Linq to Sql and multiple threads Pin
Luc Pattyn11-Jan-16 2:21
sitebuilderLuc Pattyn11-Jan-16 2:21 
AnswerRe: Memory leak trouble with Linq to Sql and multiple threads Pin
Gerry Schmitz10-Jan-16 7:42
mveGerry Schmitz10-Jan-16 7:42 
GeneralRe: Memory leak trouble with Linq to Sql and multiple threads Pin
Dave Kreskowiak10-Jan-16 8:29
mveDave Kreskowiak10-Jan-16 8:29 
GeneralRe: Memory leak trouble with Linq to Sql and multiple threads Pin
Gerry Schmitz10-Jan-16 9:04
mveGerry Schmitz10-Jan-16 9:04 
GeneralRe: Memory leak trouble with Linq to Sql and multiple threads Pin
JD8610-Jan-16 16:33
JD8610-Jan-16 16:33 
GeneralRe: Memory leak trouble with Linq to Sql and multiple threads Pin
Gerry Schmitz10-Jan-16 17:40
mveGerry Schmitz10-Jan-16 17:40 
GeneralRe: Memory leak trouble with Linq to Sql and multiple threads Pin
JD8611-Jan-16 5:01
JD8611-Jan-16 5:01 
GeneralRe: Memory leak trouble with Linq to Sql and multiple threads Pin
JD8612-Jan-16 6:24
JD8612-Jan-16 6:24 
GeneralRe: Memory leak trouble with Linq to Sql and multiple threads Pin
Luc Pattyn12-Jan-16 15:50
sitebuilderLuc Pattyn12-Jan-16 15:50 
GeneralRe: Memory leak trouble with Linq to Sql and multiple threads Pin
JD8612-Jan-16 16:29
JD8612-Jan-16 16:29 
GeneralRe: Memory leak trouble with Linq to Sql and multiple threads Pin
Luc Pattyn12-Jan-16 17:16
sitebuilderLuc Pattyn12-Jan-16 17:16 
GeneralRe: Memory leak trouble with Linq to Sql and multiple threads Pin
JD8612-Jan-16 17:30
JD8612-Jan-16 17:30 
GeneralRe: Memory leak trouble with Linq to Sql and multiple threads Pin
Luc Pattyn12-Jan-16 17:46
sitebuilderLuc Pattyn12-Jan-16 17:46 
GeneralRe: Memory leak trouble with Linq to Sql and multiple threads Pin
JD8614-Jan-16 4:07
JD8614-Jan-16 4:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.