We've recently moved our company website to a new host. It is an ASP.NET site with C# code behind, connecting to MS SQL server.
我们最近将公司网站迁移到了新主机。它是一个支持C#代码的ASP.NET站点,连接到MS SQL服务器。
Since moving the site to the new server, the site is exceeding the connection pool limit (which is not set explicitly, so I believe is at the default size of 100). Inspection of the open processes via SQL Server Management Studiorevealed that every database call appeared to be left open, and indeed, in the code I can find no explicitly closed connections at all.
自从将站点移动到新服务器后,该站点超出了连接池限制(未明确设置,因此我认为默认大小为100)。通过SQL Server管理检查打开的进程Studiorevealed每个数据库调用似乎都保持打开状态,实际上,在代码中我根本找不到明确关闭的连接。
The database connections are made in the following way:
数据库连接按以下方式进行:
DSLibrary.DataProviders.SqlProvider db = new DSLibrary.DataProviders.SqlProvider(Defaults.ConnStr);
There is precious little documentation about this DSLibrary and I assume that it's a library written by the original developer of the website. None of the class members of DSLibrary appear to explicitly close the connections either - nor are they defined within a using
block to automatically close the connection.
关于这个DSLibrary的文档很少,我认为它是由该网站的原始开发者编写的库。 DSLibrary的任何类成员似乎都没有明确地关闭连接 - 它们也没有在使用块中定义以自动关闭连接。
My question is 2 fold.
我的问题是2折。
- How would we have not encountered this problem when the site was on a different host for nearly 3 years? Is there away to automatically close unused connections that I have not implemented on the SQL server?
- 当该网站在不同的主机上将近3年时,我们怎么会没有遇到这个问题?有没有自动关闭我没有在SQL服务器上实现的未使用的连接?
- Will I be best off rewriting every connection and procedure to explicitly open and close the connection database?
- 我是否最好重写每个连接和过程以显式打开和关闭连接数据库?
UPDATE The maximum number of concurrent connections
property (Server Properties -> Connections tab) is set to 0.
UPDATE最大并发连接数属性(“服务器属性” - >“连接”选项卡)设置为0。
If I run the website in debug mode on my development machine, connecting remotely to the production database, then the connections seem to close properly. This seems to show it's something to do with the way IIS is configured?
如果我在我的开发机器上以调试模式运行网站,远程连接到生产数据库,那么连接似乎正确关闭。这似乎表明它与IIS的配置方式有关?
UPDATE 2 Setting the application pool to recycle after 30 worker processes has stopped the site exceeding maximum connections, but is now limiting some (session persistent) functionality - a recently visited items list resets very quickly and attempting to edit anything via the cms is impossible as you are logged out as soon as the processes recycle...
更新2将应用程序池设置为在30个工作进程停止站点超过最大连接后再循环,但现在限制某些(会话持久性)功能 - 最近访问的项目列表重置非常快并且尝试通过cms编辑任何内容是不可能的一旦进程回收,你就会被注销
3 个解决方案
#1
3
Most likely your code is leaking connections all over the place.
您的代码很可能是在整个地方泄漏连接。
I'd bet money your old host had the app pool set to recycle pretty often, either from a memory usage or # requests processed point. The new host most likely has default cycling in place.
我敢打赌,你的旧主机将应用程序池设置为经常回收,无论是从内存使用还是#请求处理点。新主机很可能有默认循环。
My recommendation is to first set the app pool to recycle a lot more often. Then fix the code. Either by refactoring out the DSLibrary (guessing that's home grown) or by simply changing it over to using clauses whereever you have database connections made.
我的建议是首先将应用程序池设置为更频繁地回收。然后修复代码。要么通过重构DSLibrary(猜测它的本土增长),要么通过简单地将其更改为使用子句,无论您是否建立了数据库连接。
update
One more thing, change your session properties to use sql server as the backing store that way you won't lose all the session information as the app recycles. This will buy you more time.
更新还有一件事,更改会话属性以使用sql server作为后备存储,这样您就不会在应用程序回收时丢失所有会话信息。这会给你更多时间。
#2
0
Have you checked the maximum number of concurrent connections
property (Server Properties -> Connections tab)?
您是否检查了最大并发连接数属性(服务器属性 - >连接选项卡)?
#3
0
From what you say you don't appear to be wrapping the database in a using statement so you're leaking a connection each time. The system dumps the stuff when it gets around to it unless you explicitly dispose it--which is the purpose of using. You must always use a using (or otherwise ensure dispose is called) on anything that represents a resource outside your program. Files, database connections etc.
根据您的说法,您似乎没有将数据库包装在using语句中,因此每次都会泄漏连接。除非你明确处理它,否则系统会在它转移到它时转储它们 - 这是使用的目的。您必须始终在代表程序外部资源的任何内容上使用(或以其他方式确保调用dispose)。文件,数据库连接等
#1
3
Most likely your code is leaking connections all over the place.
您的代码很可能是在整个地方泄漏连接。
I'd bet money your old host had the app pool set to recycle pretty often, either from a memory usage or # requests processed point. The new host most likely has default cycling in place.
我敢打赌,你的旧主机将应用程序池设置为经常回收,无论是从内存使用还是#请求处理点。新主机很可能有默认循环。
My recommendation is to first set the app pool to recycle a lot more often. Then fix the code. Either by refactoring out the DSLibrary (guessing that's home grown) or by simply changing it over to using clauses whereever you have database connections made.
我的建议是首先将应用程序池设置为更频繁地回收。然后修复代码。要么通过重构DSLibrary(猜测它的本土增长),要么通过简单地将其更改为使用子句,无论您是否建立了数据库连接。
update
One more thing, change your session properties to use sql server as the backing store that way you won't lose all the session information as the app recycles. This will buy you more time.
更新还有一件事,更改会话属性以使用sql server作为后备存储,这样您就不会在应用程序回收时丢失所有会话信息。这会给你更多时间。
#2
0
Have you checked the maximum number of concurrent connections
property (Server Properties -> Connections tab)?
您是否检查了最大并发连接数属性(服务器属性 - >连接选项卡)?
#3
0
From what you say you don't appear to be wrapping the database in a using statement so you're leaking a connection each time. The system dumps the stuff when it gets around to it unless you explicitly dispose it--which is the purpose of using. You must always use a using (or otherwise ensure dispose is called) on anything that represents a resource outside your program. Files, database connections etc.
根据您的说法,您似乎没有将数据库包装在using语句中,因此每次都会泄漏连接。除非你明确处理它,否则系统会在它转移到它时转储它们 - 这是使用的目的。您必须始终在代表程序外部资源的任何内容上使用(或以其他方式确保调用dispose)。文件,数据库连接等