As far as I know, there are two ways for the SQL connection handling when executing multiple commands in parallel using the Threading.Tasks.Parallel.Invoke approach. The first method is to define a common SQL connection which is shared between all threads. The second approach is to open a new connection for each thread.
Here is an example:
Threading.Tasks.Parallel.Invoke(
Sub()
Using con As New IfxConnection("ConStr")
con.Open()
Using cmd As New IfxCommand("SELECT * FROM some_table", con)
Using reader = cmd.ExecuteReader()
While reader.Read()
DoSomeStuff()
End While
End Using
End Using
End Using
End Sub,
Sub()
Using con As New IfxConnection("ConStr")
con.Open()
Using cmd As New IfxCommand("SELECT * FROM another_table", con)
Using reader = cmd.ExecuteReader()
While reader.Read()
DoMoreStuff()
End While
End Using
End Using
End Using
End Sub)
The other way would be to use one shared connection:
Using con As New IfxConnection("ConStr")
con.Open()
Threading.Tasks.Parallel.Invoke(
Sub()
Using cmd As New IfxCommand("SELECT * FROM some_table", con)
Using reader = cmd.ExecuteReader()
While reader.Read()
DoSomeStuff()
End While
End Using
End Using
End Sub,
Sub()
Using cmd As New IfxCommand("SELECT * FROM another_table", con)
Using reader = cmd.ExecuteReader()
While reader.Read()
DoMoreStuff()
End While
End Using
End Using
End Sub)
End Using
Now, which of those two ways should be preferred? Which one would result in a better performance?
As far as I observed, when using a shared connection, like in my second example, the SQL commands are getting executed sequentially. Is that true?