当多个用户同时访问同一页面时,在网站上发生错误 [英] Getting an error on website when multiple users access same page simultaneoulsy
问题描述
当多个用户访问同一页面时,我在ASP.NET 4网站上收到以下错误消息:
I'm getting the following error message on an ASP.NET 4 website when multiple users access the same page:
底层提供者无法打开。在
System.Data.EntityClient.EntityConnection.OpenStoreConnectionIf
etc ......
The underlying provider failed to open. at System.Data.EntityClient.EntityConnection.OpenStoreConnectionIf etc......
我使用实体框架4访问SQL Server 2008数据库。
I use Entity Framework 4 to access the SQL Server 2008 database.
页面有时工作,所以我知道连接字符串是正确的。此外,数据库设置为多用户,并且在连接字符串中将MARS设置为true。
The page works sometimes so I know the connection string is correct. Also the Database is set to multi user and I have MARS set to true in connection string.
在事件查看器中,我有时也会收到SQL Server消息:
Also in the event viewer I sometimes get the SQL Server message :
服务器将删除连接,因为客户端驱动程序
发送多个请求,而会话处于单用户模式。 p>
The server will drop the connection, because the client driver has sent multiple requests while the session is in single-user mode.
正如我所说,只有当我尝试通过刷新页面或点击相同的链接在两台不同的机器上同时访问同一页面时才会发生
As I say, this only happens if I try accessing the same page simultaneously on two different machines by just refreshing the page or clicking the same link.
任何帮助都将不胜感激。
Any help would be much appreciated.
添加连接字符串(第一个为ASPNETDB,第二个为主数据库):
Connection strings added (first one for ASPNETDB and second for main database):
<add name="ApplicationServices" connectionString="Data Source=MyServer;Initial Catalog=MyDB;Integrated Security=False;Persist Security Info=False;User ID=****;Password=****;Connect Timeout=120" />
<add name="MyDBEntities" connectionString="metadata=res://*/App_Code.MyDBModel.csdl|res://*/App_Code.MyDBModel.ssdl|res://*/App_Code.MyDBModel.msl;provider=System.Data.SqlClient;provider connection string="Data Source=MyServer;Initial Catalog=MyDB;Integrated Security=False;User ID=****;Password=****;Connect Timeout=120;User Instance=false;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
我用于访问上下文的类如下:
The Class I use to access the context is as follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MyDBModel;
using System.Web.Security;
/// <summary>
/// Summary description for MyDataAccess
/// </summary>
public static class MyDataAccess
{
// Private Class Members
//-----------------------------------------------------------------------------------------
private static MyDBModel.MyDBEntities dal = new MyDBModel.MyDBEntities();
// Class Constructor / Destructor
//-----------------------------------------------------------------------------------------
static MyDataAccess()
{
// Set Entity ObjectContext Merge Options - this basically ensures database items aren't cached
dal.NEWS.MergeOption = System.Data.Objects.MergeOption.OverwriteChanges;
dal.NEWS_IMAGES.MergeOption = System.Data.Objects.MergeOption.OverwriteChanges;
}
// Public Methods for Data Access
//-----------------------------------------------------------------------------------------
/// <summary>
/// Get All Current News Items
/// </summary>
public static List<NEWS> GetCurrentNewsItems()
{
var items = from news in dal.NEWS
where news.NEWS_ACTIVE == true &&
news.NEWS_STARTDATE <= DateTime.Today &&
news.NEWS_EXPIRYDATE >= DateTime.Today
orderby news.NEWS_DATE descending
select news;
return NewsManager.GetMyNewsItems(items).ToList();
}
}
然后在我的.aspx.cs页面我将使用以下内容:
Then within my .aspx.cs page I would use something like:
var news = MyDataAccess.GetCurrentNewsItems();
推荐答案
我相信你的问题是由存储你的ObjectContext静态变量中的对象。
I believe your issue is being caused by storing your ObjectContext object in a static variable.
ObjectContext类不是线程安全的。在多线程
方案中,无法确保ObjectContext中数据
对象的完整性。
The ObjectContext class is not thread safe. The integrity of data objects in an ObjectContext cannot be ensured in multithreaded scenarios.
这篇关于当多个用户同时访问同一页面时,在网站上发生错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!