Действительно ли возможно, получает sql утечки соединения с помощью LINQ?

Если вы хотите посмотреть на различные inv_id, вам просто нужно добавить еще одну группу, например:

 SELECT
order_id,
inv_id,
SUM(qty*order_price) as Order_Total
FROM alp_orderline
GROUP BY
nv_id,
rder_id
HAVING Order_Total > 100
ORDER BY Order_Total ASC;

Вы также можете использовать подзапрос, а затем во внешнем запросе вы получите условие, в котором
Order_Total> 100

6
задан Atle 23 April 2009 в 06:35
поделиться

3 ответа

When your DataContext is not disposed of and stays alive, the associated connection will stay alive too. Database connections are unmanaged resources and all unmanaged resources must be disposed of properly.

Even if you use delay-loading and do not have a well-defined scope, you should still clean up database connections at the end of a logical unit of work. In ASP.NET apps, the latest possible moment for this would be at the end of request processing - in the Application_EndRequest method of the Globals.asax file. In a WCF service, any active data context should be disposed of at the end of every service method call.

The documentation for this is vague and while most of the time, you can get away with not disposing your DataContext, there do appear to be some scenarios where the data loaded from a connection is keeping the connection itself alive. The easiest way to confirm that this is happening in your case is to test it.

10
ответ дан 8 December 2019 в 17:27
поделиться

Я обнаружил, что после еще нескольких поисков я нашел этот вопрос и ответ , где говорится, что linq можно одурачить, чтобы оставить соединение открытым.

Я сделал это небольшой тестовый код, который воспроизводит его. Если я просто заменяю Enumerator на foreach, он работает нормально, но он Enumerator сохраняет соединения открытыми.

public Organisation RunTestQuery2()
{
    IEnumerable<Organisation> orgs  = base.GetEntities<Organisation>().Take(5);

    var enumerator = orgs.GetEnumerator();
    int i = 0;


    while (enumerator.MoveNext())
    {
        var org = enumerator.Current;
        Debug.WriteLine(org.DescribingName);
        if (i == 3)
        {
           return org;
        }
        i++;
    }

    return null;
}

Если я добавлю вызов для удаления из контекста, они исчезнут.

4
ответ дан 8 December 2019 в 17:27
поделиться

Are you getting any deadlocks in your database? A quick look at the Activity Monitor should give you some indication.

What do you do to manage DataContext lifecycle - what sort of application have you written (a website, a Windows Client, other)?

Once used in a query or operation, a DataContext will keep a connection so that entities loaded can lazy load & etc, so it's imperative that you plan how you use DataContexts in your application.

WCF services.. In that case, I am a big fan of the "one context per request" approach. I'd encourage you to wrap your data operations within a using() statement so that the context is disposed when you're done.

0
ответ дан 8 December 2019 в 17:27
поделиться
Другие вопросы по тегам:

Похожие вопросы: