在Web服务中处理sql连接的最佳方法?

发布于 2024-07-10 21:15:50 字数 745 浏览 5 评论 0原文

我有一个最多由 10 个客户端调用的 Web 服务。 该网络服务由 7 个不同的 asmx 页面组成,每个页面大约有 100-200 个功能。

所有这些功能都适用于 MSSQL2005 或 MS SQL2000 数据库。 一天中的某些时段,来自客户端的流量很大,似乎我的 sql 服务器上的连接耗尽,导致所有客户端停止。

在每个函数中,我打开一个连接,执行一些操作,然后关闭连接,有时有事务,有时没有。

在服务器上,我看到它创建了很多连接,我不知道为什么它们没有消失,但即使在功能完成后仍然保留在那里。 所以我可以看到我的 10 个客户端不时创建超过 80 个连接。 有时它们中的一些会消失,有时它们在使用数小时后仍然存在。 那里正在进行某种汇集吗?

问题一: 是否有另一种方法来处理我应该使用的连接,例如每个 Web 服务全局一个连接或任何其他方式?

问题2: 如果可以处理每个函数的连接,那么为什么它不关闭服务器上的连接,使打开的连接列表一直变得越来越大,直到我摆脱连接错误?

这个问题与我的其他问题相关,但不相同: 奇怪SQL2005问题。 “SqlConnection 不支持并行事务”

我现在已将其范围缩小到“连接不足”错误。

I have a webservice that is called by up to 10 clients. The webservice is built up of 7 differnet asmx-pages and have about 100-200 functions in each page.

All those functions are working against a MSSQL2005 or MS SQL2000 database. Some periods of the day its heavy traffic from the clients and it seems like I run out of connections on the sql-server causing all clients to stop.

In every function I open a connection, do the stuff and then close the connection, sometimes with transactions sometimes without.

On the server I see that it creates a lots of connections, I dont know why they dont goes away, but stays there even after the function is done and over. So I can se that my 10 clients creates over 80 connections from time to time. Sometimes some of them goes away, sometime they are still there hours after used. Is there some kind of pooling going on there?

Question 1:
Are there another way to handle connections I should use, like one connection globally per webservice or any other way?

Question 2:
If its ok to handle connections per function then why does it not close the connection on the server, making the list of open connections larger and larger all the time until I got out of connections-error?

This question is related to my other question but not the same: Strange SQL2005 problem. "SqlConnection does not support parallel transactions"

I have now narrowing it down to the "out of connections" error.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

饭团 2024-07-17 21:15:50

我同意有关重构的评论,但这不是这里的相关问题。

您绝对应该在每个函数中使用连接,听起来您没有正确处理它们。 假设数据库操作包含在您正在调用的函数中,您的代码应如下所示:

using (SqlConnection connection = <connection code>)
{
  using (SqlCommand command = <command code>)
  {
    // Execute.
  }
}

在服务器端,连接将保持打开状态。 默认情况下,SqlConnection 类启用连接池,因此您将看到服务器端打开的连接。

这种行为是正常的,应该是可以预料到的。

I agree on the comment about refactoring, but that's not the pertinent issue here.

You definitely should use a connection in each function, and it sounds like you aren't disposing of them correctly. Assuming the database operations are contained to the function you are calling, your code should look something like this:

using (SqlConnection connection = <connection code>)
{
  using (SqlCommand command = <command code>)
  {
    // Execute.
  }
}

On the server side, the connections are going to stay open. By default, the SqlConnection class enables connection pooling, so you are going to see the connection open on the server end.

This behavior is normal and should be expected.

要走就滚别墨迹 2024-07-17 21:15:50

您实际上看到了正在运行的连接池。 默认情况下,在 .Net 2+ 到 SQL 2005 中会发生这种情况(不确定其他版本)。

连接池意味着 .Net 将为您保留一些打开的连接,以便在您下次需要连接时减少开销。 CLR 可以为您提供一个已经打开(并已清理)的连接,这比直接重新连接到数据库快几百倍。 当您调用 connection.Close() 时,您只是将连接交还给池以供回收。

每个单独的安全上下文连接都存在一个池 - 这意味着使用相同 SQL 安全性建立的所有连接将共享一个池,而如果您使用 Windows 身份验证,则每个单独的用户连接将拥有自己的池。

您遇到问题是因为您达到了 100 个连接 - 这是池中的默认最大数量(默认最小值为 0)。 此时,池将无法提供另一个连接,直到一个连接被回收,因此应用程序将挂起或超时。 您需要更改连接字符串以包含更高的最大数量(并考虑减少连接)。

You are actually seeing connection pooling in operation. This happens by default in .Net 2+ to SQL 2005 (not sure about other versions).

Connection pooling means .Net will hold open a few connections for you so that there is less overhead when you next require a connection. The CLR can just given you an already open (and cleaned up) connection which is a few hundred times quicker than reconnection to the database directly. When you call connection.Close() you are just handing back the connection to the pool for recycling.

A pool exists for every separate security context connection - this means that all connections made using the same SQL security will share a pool, whereas if you are using Windows authentication, each individual user connecting will have their own pool.

You are seeing issues because you are hitting 100 connections - the default maximum amount in the pool (the default minimum is 0). At this point the pool will not be able to provide another connection until one has been recycled, hence the application will hang or time out. You need to change your connection string to include a higher maximum number (and also look at cutting down your connections).

茶底世界 2024-07-17 21:15:50

那么单个 Web 服务中有 700-1400 个功能? 对我来说听起来太大了。 是时候重构了。

我不知道页面和网络服务之间的联系是什么。 我通常将它们视为 XML 消息传递端点,与使用它们的客户端完全分离。

So that's 700-1400 functions in a single web service? Sounds too big to me. Time to refactor.

I don't know what the connection between pages and web services is. I usually think of them as XML messaging endpoints, completely separate from the client that consumes them.

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