Imports System.Data.SqlClient
Module Module1
Sub Main()
Dim iCount As Integer = 1
Try
Do
Dim sqlConn As New _
SqlConnection("Data Source=localhost;trusted_Connection=yes;initial catalog = MyDatabase;max pool size =100;")
sqlConn.Open()
Trace.WriteLine("opening connection " & CStr(iCount))
'sqlConn.Close()
'Trace.WriteLine("closing connection " & CStr(iCount))
'sqlConn.Dispose()
'Trace.WriteLine("disposing connection " & CStr(iCount))
iCount = iCount + 1
Loop Until iCount > 20000
Catch ex As Exception
Trace.WriteLine(ex.ToString)
End Try
End Sub
End Module
如果我执行此代码,此测试应用程序会在引发The timeout period elapsed prior to obtaining a connection from the pool
异常之前打开115个与数据库的连接。但是应用程序池中只有100个连接。怎么可能?
答案 0 :(得分:3)
猜猜:既然您没有持有对开放连接对象的引用,那么前15个可能会被处置并重用?如果你将连接推到List<>
上,你还能得到115吗?