这段代码会杀死我的服务器吗?
我的网站一直存在问题,它基本上超时并消失。我现在已经到了必须将应用程序池设置为每 5 分钟自动回收一次的地步,但即使这样也失败了,因为我刚刚下班回来,我的电子邮件收件箱充满了 4000 封电子邮件,所有电子邮件都具有相同的内容错误。
System.Data.SqlClient.SqlException:超时已过期。操作完成之前超时时间已过,或者服务器未响应。
今天早上我尝试了一个测试,在连接字符串上禁用池化,这也不起作用。
现在我在想,也许这不是连接泄漏的问题,我以前经历过所有这些,我认为这可能与我网站的核心静态属性有关,
是其中之一
public static List<Member> AllMembers
{
get
{
if (HttpRuntime.Cache["Members"] != null)
{
return (List<Member>)HttpRuntime.Cache["Members"];
}
else
{
GetAllMembers();
return (List<Member>)HttpRuntime.Cache["Members"];
}
}
}
这 每当我想要成员列表时就会调用,您可以看到,如果它为空,它将填充缓存,这将使用数据库,如果它不为空,那么它将返回缓存对象。我也有 SQLCacheDependancy ,它将清除这些缓存对象,以便再次填充它们。所以这个属性被称为很多。
现在这是一个网络应用程序,并且随着我的流量一直在增加其染色,
我的属性可能是原因吗?
非常感谢
Truegilly的任何帮助
Im having an ongoing issue with my site, it basically times out and dies. I have gotten to the point now where I have had to set the application pool to auto recycle every 5 minutes, but even that has failed as I’ve just got back from work and my email inbox is full of 4000 emails all with the same error.
System.Data.SqlClient.SqlException: Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.
This morning I tried a test where I disabled pooling on the connection string, this also didn’t work.
Now I'm thinking that perhaps this isn’t an issue with the leaky connections, I've been through all that before, I think it maybe something to do with the static properties that are core to my site
Here is one of them
public static List<Member> AllMembers
{
get
{
if (HttpRuntime.Cache["Members"] != null)
{
return (List<Member>)HttpRuntime.Cache["Members"];
}
else
{
GetAllMembers();
return (List<Member>)HttpRuntime.Cache["Members"];
}
}
}
This is called whenever I want a list of the members, you can see that if its null it populates the cache, which will use the database, and if it’s not null then it will return the cache object. I also have SQLCacheDependancy too which will clear these cache objects so it will again populate them. So this property gets called ALOT.
Now this is a web application and as my traffic has increased its dyeing all the time,
Could my properties be the cause?
Any help is most appreciated
Truegilly
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
假设您正确处理了所有内容,我有一个替代解释:
如果缓存为空/过期并且多个页面尝试同时调用
AllMembers
,那么每个页面最终可能会调用GetAllMembers()
同时进行,减慢数据库查询速度。如果调用开始超时,这可能会启动恶性循环。您可以在代码周围放置一个
锁
,这样每个属性只能进行一次数据库查询。我可以这样设置:Assuming you are disposing of everything correctly, I have an alternative explaination:
If the cache is empty/expired and multiple pages try to call
AllMembers
at the same time, then each page may will end up callingGetAllMembers()
simultaneously, slowing down the database query. This could start a vicious cycle if the calls start timing out.You could place a
lock
around your code, so only one database query per property can be made. Here is how I might set it up:只是一些常见的怀疑:
您是否处置了所有 SqlCommand 和读取器?
您是否正在处置/关闭您的 SqlConnection?
您的数据库能够处理负载吗?您那里有性能问题吗?
您帖子中的代码看起来不像问题。
Just a few usual suspects:
Are you disposing all your SqlCommand's and readers?
Are you disposing/closing your SqlConnection's?
Is your database able to handle the load? Do you have performance problems there?
The code from your post doesn't look like the problem.
我在这里没有看到任何保护措施来防止对 getter 的多个并发调用触发
GetAllMembers
。当然,您一次只需要一个线程来加载该属性。似乎需要某种lock()
。您应该确保刷新缓存的代码也使用相同的
lock()
。如果可以从多个服务器调用此代码,您还需要确保支持
GetAllMembers
的任何数据库端程序逻辑(存储过程或来自客户端的动态 SQL)有效且正确地处理多个并发读者。I don't see any guard here to prevent multiple concurrent calls to the getter triggering
GetAllMembers
. Surely you only want one thread at a time to load the property. Some kind oflock()
appears to be required.You should make sure that the code that flushes the cache also uses the same
lock()
.If this code can be called from multiple servers, you also need to make sure that any database-side program logic (Stored procedures, or dynamic SQL from the client) that underpins
GetAllMembers
efficiently and correctly handles multiple concurrent readers.