I have recently become aware of this issue: https://blogs.msdn.microsoft.com/spike/2008/09/17/nested-recordset-and-the-portsocket-in-time_wait-problem-by-example/
Where if you open a recordset, then use the connection for something else with the recordset still open, the web server opens a new port to talk to the DB, then immediately puts that port into the TIME_WAIT state.
I have been going through the site to fix this (by closing recordsets before the connection is re-used - it works) but noticed something odd.
When the page has a "Response.Flush" on it, no matter WHAT you do, it causes an extra port to be used and then be put into the useless TIME_WAIT state. This can lead to a serious case of port exhaustion.
SAMPLE CODE:
<%@ Language=VBScript %>
<html>
<head>
</head>
<body>
<%
response.flush
set cnn = server.CreateObject("adodb.connection")
CNN.cursorlocation=3
cnn.open [YOUR CONNECTION STRING]
set rs=server.createobject("adodb.recordset")
set rs=cnn.execute("select top 1 * from [YOUR TABLE]")
cnn.close
set cnn=nothing
%>
</body>
</html>
To check for time waits you can run:
netstat -nao | find /i "[YOUR DB IP]" /c
via the command prompt on the web server. Assuming this is a test system, you should immediately see time_wait connections popup from your web server to your DB server. Remove the flush, and this stops.
Googling has not helped - open to any suggestions.
Environment: IIS 7.5, Classic ASP, SQL Server 2008.
EDIT:
I also tried this as per the comments:
Set cmd = Server.CreateObject("ADODB.Command")
With cmd
'No need to handle connection let ADODB.Command create and destory it.
.ActiveConnection = sqlcnnstr1
.CommandType = 1
.CommandText = "select top 1 * from [YOUR_TABLE]"
Set rs = .Execute()
If Not rs.EOF Then data = rs.GetRows()
Call rs.Close()
Set rs = Nothing
End with
Set cmd = Nothing
Still same issue. With the flush, extra connection, without, same connection.
EDIT 2 Turns out its got nothing to do with database. If you have JUST a response.flush, nothing else, that already causes an extra connection, from IIS to the client, so I've been looking at the wrong of things.
The question now becomes can you get away with not having response.flush spawn an extra connection from IIS to the client.