📜 ⬆️ ⬇️

LINQ to SQL errors on load sites (> 30 users)

Most recently, I started using LINQ to SQL and, with loads on the site, began to notice these kinds of errors,
At what appear in the style of the Korean Random-a:

option 1) InvalidCastException
option 2) DataReader is closed
option 3) SQL Server also generates an error associated with MARS and tears up the connection


As usual, the class looked like working with LINQ:
')
using System.Linq;
using Kushatpodano.Ru.Models;

namespace Kushatpodano.Ru.Classes.BusinessLayer
{
public class CustomPages
{
private readonly KushDBDataContext context = new KushDBDataContext();

public static string PageText( string ActionResult)
{
return ( from p in context.CustomPages
where p.ActionResult == ActionResult
select p).Single().PageText;
}

public static string PageTitle( string ActionResult)
{
return ( from p in context.CustomPages
where p.ActionResult == ActionResult
select p).Single().PageTitle;
}
}
}


* This source code was highlighted with Source Code Highlighter .


Searching for these errors in Google practically did not give anything, but yesterday I stumbled upon another topic on the forum with this error, where it was written how it was bypassed, after rewriting the classes that way - the hosting provider stopped holding the load, but LINQ was doing well:

using System.Linq;
using Kushatpodano.Ru.Models;

namespace Kushatpodano.Ru.Classes.BusinessLayer
{
public class CustomPages
{
public static string PageText( string ActionResult)
{
string PageText;
using (KushDBDataContext context = new KushDBDataContext())
{
PageText = ( from p in context.CustomPages
where p.ActionResult == ActionResult
select p).Single().PageText;
}

return PageText;
}

public static string PageTitle( string ActionResult)
{
string PageTitle;
using (KushDBDataContext context = new KushDBDataContext())
{
PageTitle = ( from p in context.CustomPages
where p.ActionResult == ActionResult
select p).Single().PageTitle;
}

return PageTitle;
}
}
}


* This source code was highlighted with Source Code Highlighter .


For each block of appeals, a new context should be created and destroyed,
as in a case when we work with normal SqlConnection.

Source: https://habr.com/ru/post/67207/


All Articles