|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
are System.Data.SqlClient.SqlConnection thread safe? can many threads share a System.Data.SqlClient.are System.Data.SqlClient.SqlConnection thread safe? can many threads share
a System.Data.SqlClient.SqlConnection instance without any synchronization? Hi Daniel,
I remember a colleague was telling me it was not thread-safe and his code was experiencing performance hits. Actually, since there's the connection pool being maintained behind the scenes, there's no need to share a SqlConnection instance. Show quote "Daniel" <softwareengineer98***@yahoo.com> wrote in message news:eN7fzUXTGHA.4864@TK2MSFTNGP12.phx.gbl... > are System.Data.SqlClient.SqlConnection thread safe? can many threads > share > a System.Data.SqlClient.SqlConnection instance without any > synchronization? > > I think it is thread safe.. sort of, as now it supports MARS:
From http://blogs.msdn.com/angelsb/archive/2004/07/15/184479.aspx: " Multiple Active Result Sets per connection (MARS): I don't really believe this feature will be the winner of the PTBGM, but it is definitely a contender. When connecting with SqlClient to Sql Server 2005 (or with Oledb and the next release of the network libraries) we will now allow multiple active result sets per connection. In ado.net this mostly means that you will now be able to open multiple datareaders per connection as long as you use a separate command for each. The main scenario for this feature would be being able to modify the database as you process a datareader, it also enables running queries inside of the same transaction context and _can_ have a performance benefit in very specific scenarios where the cost of more connections (about 40k memory per connection) becomes a concern. There are a number of ways in which "things can go wrong" T however. Forgetting to close a datareader will now stop you from committing a transaction, modifying the state of the connection can give you unexpected results, reading and writing inside of a transaction context may isolate the reader from the changes and writer writer conflicts are non deterministic. Overall fairly minor and contrived concerns, my personal biggest problem with this feature however is that it is hard to squeeze real performance gains out of it, and easy to write hard to maintain code to try it. There are performance implications to using MARS across the board. On the client we run into an issue where creating a new batch is not free, we kind of work around this issue by pooling mars commands but still expensive if you don't used the pooled functionality. On the network layer there is a cost associated with multiplexing the tds buffer, opening too many batches can be more expensive than opening another connection. On the server all MARS batches run in the same scope, worst case scenario your queries will end up running in the order received. I really like the current way to use ado.net, open a new connection in each thread and rely on pooling to get performance and ease of development. It is tempting with the advent of mars to switch this model to one where we open one connection and rely on using a different command in each thread, I would not recommend it. Misusing (IMO) MARS to try to get additional performance has the potential of making your code look like a bowl of spaghetti for very unclear performance benefits. My opinion: Don't try to use MARS and threading to improve performance. " The last paragraph seems to say that, yes, it is thread safe, but please do not use it. And I do agree. Connection pooling is way more efficient. Laura Show quote "Dmytro Lapshyn [MVP]" <x-code@no-spam-please.hotpop.com> ha scritto nel messaggio news:eLGo$cYTGHA.4956@TK2MSFTNGP09.phx.gbl... > Hi Daniel, > > I remember a colleague was telling me it was not thread-safe and his code > was experiencing performance hits. > > Actually, since there's the connection pool being maintained behind the > scenes, there's no need to share a SqlConnection instance. > > "Daniel" <softwareengineer98***@yahoo.com> wrote in message > news:eN7fzUXTGHA.4864@TK2MSFTNGP12.phx.gbl... >> are System.Data.SqlClient.SqlConnection thread safe? can many threads >> share >> a System.Data.SqlClient.SqlConnection instance without any >> synchronization? >> >> > Thus wrote Laura T.,
> I think it is thread safe.. sort of, as now it supports MARS: No. MSDN clearly states that it isn't, and the availability of MARS has little to do with the question of thread-safety. Cheers, -- Joerg Jooss news-re***@joergjooss.de Yes, MSDN clearly states that instance methods are not thread safe.
But since the object can be used by many threads contemporarly, MARS, it should has some safeguards. Connection can be common for many data readers. It still does not make connection class fully thread safe. Laura Laura Show quote "Joerg Jooss" <news-re***@joergjooss.de> ha scritto nel messaggio news:94fc50711f1ab8c81bfff4b5c6de@msnews.microsoft.com... > Thus wrote Laura T., > >> I think it is thread safe.. sort of, as now it supports MARS: > > No. MSDN clearly states that it isn't, and the availability of MARS has > little to do with the question of thread-safety. > Cheers, > -- > Joerg Jooss > news-re***@joergjooss.de > > Thus wrote Laura T.,
> Yes, MSDN clearly states that instance methods are not thread safe. Why? MARS allows you to open multiple DataReaders over a single connection. > But since the object can be used by many threads contemporarly, MARS, > it > should has some safeguards. That's it. The assumption that this connection therefore can be shared among multiple threads is dangerous one. Cheers, -- Joerg Jooss news-re***@joergjooss.de
Other interesting topics
|
|||||||||||||||||||||||