A
A
Andrey Shamov2013-01-17 14:44:52
.NET
Andrey Shamov, 2013-01-17 14:44:52

Is ADO.NET thread safe?

Is ADO.NET thread safe?
A simple query like:

Dim Connection As SqlClient.SqlConnection = New SqlClient.SqlConnection(ConnectionSettings.Connect­ionString)
Dim Command As System.Data.SqlClient.SqlCommand
Connection.Open()
Command = RequestBuilder.GetSomeData() 'тут заполняется commandtext и параметры запроса
Command.CommandTimeout = FCommandTimeout
Command.Connection = Connection
DataSetAdapter = New SqlClient.SqlDataAdapter(Command)
DS = New System.Data.DataSet("GetSomeData")
Try
  DataSetAdapter.Fill(DS)
Finally
  If Connection IsNot Nothing AndAlso Connection.State = ConnectionState.Open Then 
    Connection.Close()
  End If
End Try

Returns completely "left" data, possibly from a query that is executed in a parallel thread (the defect only manifests itself in multi-threaded work). How to fight and why is it so?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Seter17, 2013-01-17
@Seter17

here below it says that ado.net is not thread-safe, except for reading from a dataset. So you really need to think about the distinctions

M
mayorovp, 2013-01-18
@mayorovp

Since you are creating a new Connection each time, thread safety is fine.
Look for an error in RequestBuilder.GetSomeData()

S
Sergey Gorbushin, 2013-04-30
@setsergey

Do you use temporary tables (#table) in your SQL query?
The fact is that temporary tables live within the same connection, and SQL Server, after closing the connection, does not kill it immediately, but keeps it for some time (this is done to increase performance). Thus, with frequent queries, SQL Server can give such a connection to all temporary tables ...
The way out of this situation is to delete everything from temporary tables in the SQL query if they exist.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question