I'm experimenting with async query execution for some time.My goal is executing simple SQL statements and dont wait them to finish. The below code works well for 10, 500 or 1000 or even 5000 queries. but for 50000 queries suddenly error comes up and says
"BeginExecuteReader requires an open and available Connection. The connection's current state is open." and sometimes it says "... state is: broken"
this is aspnet test site and I think 50.000 queries can happen. is it me missing something ? shouldn't it work ?
I use windows7 x64 and I belive its something todo with sql connection polling limits. you'll possibly say that 50.000 is too high but I need to avoid this error to trust the code and I don't know how.
ps: In code I open connection but don't close it for test purposes. if I close connection callback function never fires.
any suggestions ? And there are not too much information about this error on google.
Partial Class test
Inherits System.Web.UI.Page
Dim cnTest As SqlConnection
Protected Sub cmdAsyncTest_Click(sender As Object, e As EventArgs) Handles cmdAsyncTest.Click
Dim s As String = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
Dim sqlstr As String
Dim x1 As Integer, x2 As Integer, i As Integer
sqlstr = "INSERT INTO test1 (name,surname,a2) VALUES ('" & s & "','" & s & "',5)"
Dim cnstr As String = System.Configuration.ConfigurationManager.ConnectionStrings("ConnectionStringLOG").ConnectionString
cnTest = New SqlConnection(cnstr)
cnTest.Open()
watch = Stopwatch.StartNew()
For i = 0 To 50000
myExecute_Async(sqlstr)
Next
End Sub
Function myExecute_Async(ByVal sqlstr As String) As String
Using cmd As New SqlCommand(sqlstr, cnTest)
cmd.CommandType = CommandType.Text
cmd.BeginExecuteReader(New AsyncCallback(AddressOf QueryCallback), cmd)
Return ""
End Using
End Function
Sub QueryCallback(ByVal async As IAsyncResult)
' ToDo: something
End Sub
End Class
CREATE TABLE [dbo].[test1](
[ID] [int] IDENTITY(1,1) NOT NULL,
[name] [varchar](50) NULL,
[surname] [varchar](50) NULL,
[a2] [int] NULL,
CONSTRAINT [PK_test1] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
".NET SqlConnection class, connection pooling and reconnection logic" article is not the answer. my problem is with async execution.
I tried to use that code. I tried not to use sun routines:
Dim cnstr As String = System.Configuration.ConfigurationManager.ConnectionStrings("ConnectionStringLOG").ConnectionString
cnTest = New SqlConnection(cnstr)
cnTest.Open()
watch = Stopwatch.StartNew()
For i = 0 To 50000
Using cmd As New SqlCommand(sqlstr, cnTest)
' Return "" & cmd.ExecuteNonQuery()
cmd.CommandType = CommandType.Text
cmd.BeginExecuteReader(New AsyncCallback(AddressOf QueryCallback), cmd)
End Using
Next
this time I got Exception of type 'System.OutOfMemoryException' was thrown. at the cmd.BeginExecuteReader line.
lets say I have logic that needs to run this 50.000 commands. what should I do to avoid memory problems or pooling limits ?