是的SqlConnection /的SqlCommand线程安全的? [英] Is SqlConnection / SqlCommand thread safe?

查看:146
本文介绍了是的SqlConnection /的SqlCommand线程安全的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在创建一个WCF Web服务。



作为其工作的一部分,这将不幸需要做一些相当密集的计算,但这些计算可能还好是调用web服务之间共享。实际上 - 我们只需要做的计算一次,所有以后调用可以得到的好处



不过,由于WCF没有共享的应用程序状态下,它似乎是合乎逻辑设置WCF在单实例模式。 (每个客户端都需要一些计算在所有的情形产生,迫使我们重新计算他​​们每serssion其中的可能的是好的,或者每次呼叫这是站不住脚的)



不过,我不是很熟悉,确保代码的多个线程。
我已在它的一些读了起来,并没有我们的WCF的代码写入到共享状态,我几乎确信我不需要改变(比计算位,这是很容易保护其它)任何东西。



有一个单一的障碍,但 - 我们使用SqlConnection和SqlCommand时使用我们的后台进行沟通,我不知道我是否可以在这些是线程计算安全



编辑:我也许应该澄清的命令/连接是局部的方法。

 使用sqlConn =新的SqlConnection(...){
:我们在静脉交谈模式尝试{
sqlConn.Open()
}赶上(){
抛出新的FaultException();
}
变种CMD =新的SqlCommand(一些SQL,sqlConn);
变种读者= cmd.ExecuteReader();
//读取东西
reader.Close();
//返回的东西
}



编辑完



我抬头SqlCommand类MSDN上:
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.aspx
它说:任何公共静态(这种类型的在Visual Basic中的Shared)成员都是线程安全的。所有实例成员不能保证是线程安全的。



我是不是正确的思维解释这一点意味着, MS做的的保证的SqlCommand在一个多线程的情况下工作?



如果没有,是否有一个线程安全的替代方案?



是的,我可以只锁定所有)



干杯提前:数据库访问在我的web b)如没有必要我宁愿我没得方法,但一)它的丑陋和!


解决方案

我是不是正确的思维就意味着MS不保证的SqlCommand工作在解释本一个多线程的情况?




它在多线程的情况下正常工作,只要你正确地使用它。



如果多个线程试图使用的同一个的的SqlCommand,你怎么想会发生什么?它怎么能工作呢?



但是,如果使用不同的连接不同的线程发出不同的命令到同一个数据库,是没有问题的。



有关MSDN线程安全的音符真的破裂,严重的措辞,必须由人谁不知道什么是线程安全写的



他们正在试图与该消息说(这是上涨到记录MSDN上的类和函数的99.9%),那是什么这一类的任何静态方法,可以安全地同时由多个被称为线程。任何实例成员的在同一实例的是不能保证是安全的,如果由多个线程并发调用,但访问同一个部件上的不同的的对象是完全正常的。


I am currently creating a WCF web service.

As part of its job, it will unfortunately need to do some fairly intensive computations, however these computations can fortunately be shared between calls to the webservice. In effect - we only need to do the computations once, and all later calls can get the benefit.

However, since WCF has no shared application state it seems logical to set WCF in single-instance mode. (Each client would require some of the computations in all likelyhood, forcing us to recompute them per-serssion which could be ok, or per-call which is untenable)

However, I am not very familiar with securing code for multiple threads. I have been reading up on it some, and as none of our WCF code writes to shared state (other than the computation-bit, which is easy to protect) I'm almost convinced I don't need to change anything.

There is a single snag, though - we use SqlConnection and SqlCommand to communicate with our backend, and I am not sure if I can count on these being thread safe?

EDIT: I should perhaps clarify that the Commands / Connections are always local to a method. We're talking a pattern in the vein of:

using sqlConn = new SqlConnection(...) {
 try {
  sqlConn.Open()
} catch () {
  throw new FaultException();
}
var cmd = new SqlCommand("Some SQL", sqlConn);
var reader = cmd.ExecuteReader();
//Read the stuff 
reader.Close();
//Return something
}

END EDIT

I looked up the SqlCommand class on MSDN: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.aspx which says: "Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe."

Am I interpreting this correctly in thinking it means that MS does not guarantee that SqlCommand works in a multi threaded scenario?

If it does not, is there a thread-safe alternative?

Yes, I could just lock all database access methods in my webservice, but a) it's ugly and b) if it's not necessary I'd prefer I didn't have to :)

Cheers in advance!

解决方案

Am I interpreting this correctly in thinking it means that MS does not guarantee that SqlCommand works in a multi threaded scenario?

It works fine in a multi-threaded scenario as long as you use it correctly.

If several threads try to use the SAME SqlCommand, what do you think will happen? How could it possibly work?

but if different threads using different connections issue different commands to the same database, there is no problem.

The notes about thread safety on MSDN are really broken and badly worded, and must be written by someone who didn't know what thread safety is.

What they're trying to say with that message (which is tacked onto 99.9% of the classes and functions documented on MSDN) is that "Any static method of this type can be safely called concurrently by multiple threads. Any instance members on the same instance is not guaranteed to be safe if invoked concurrently by multiple threads, but accessing the same member on different objects is perfectly fine."

这篇关于是的SqlConnection /的SqlCommand线程安全的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆