这段代码会杀死我的服务器吗?

发布于 2024-09-27 15:14:09 字数 896 浏览 0 评论 0原文

我的网站一直存在问题,它基本上超时并消失。我现在已经到了必须将应用程序池设置为每 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

分开我的手 2024-10-04 15:14:09

假设您正确处理了所有内容,我有一个替代解释:

如果缓存为空/过期并且多个页面尝试同时调用 AllMembers ,那么每个页面最终可能会调用 GetAllMembers() 同时进行,减慢数据库查询速度。如果调用开始超时,这可能会启动恶性循环。

您可以在代码周围放置一个,这样每个属性只能进行一次数据库查询。我可以这样设置:

private static object _allMembersLock = new object();
public static List<Member> AllMembers
{
    get
    {
        lock (_allMembersLock)
        {
            List<Member> members = (List<Member>)HttpRuntime.Cache["Members"];
            if (members == null)
            {
                members = GetAllMembers();
                HttpRuntime.Cache["Members"] = members;
            }
            return members;
        }
    }
}

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 calling GetAllMembers() 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:

private static object _allMembersLock = new object();
public static List<Member> AllMembers
{
    get
    {
        lock (_allMembersLock)
        {
            List<Member> members = (List<Member>)HttpRuntime.Cache["Members"];
            if (members == null)
            {
                members = GetAllMembers();
                HttpRuntime.Cache["Members"] = members;
            }
            return members;
        }
    }
}
吃素的狼 2024-10-04 15:14:09

只是一些常见的怀疑:

  1. 您是否处置了所有 SqlCommand 和读取器?

  2. 您是否正在处置/关闭您的 SqlConnection?

  3. 您的数据库能够处理负载吗?您那里有性能问题吗?

您帖子中的代码看起来不像问题。

Just a few usual suspects:

  1. Are you disposing all your SqlCommand's and readers?

  2. Are you disposing/closing your SqlConnection's?

  3. 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.

黎夕旧梦 2024-10-04 15:14:09

我在这里没有看到任何保护措施来防止对 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 of lock() 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文