is Microsoft.Practices.EnterpriseLibrary.Data.Database.SetParameterValue thread safe?
I'm using a Database to run some queries and was thinking to optimize stuff up.
I am basically doing two static objects:
private static Database db = ... ;
private static开发者_如何学JAVA DbCommand cmd = db.GetSqlStringCommand("... where col = @colParam");
....
cmd.Prepare(); //one call
And then was thinking to just do:
db.AddInParameter(cmd, "colParam", DbType.String, "some value on each call")
when calling it.
It only works one time. After the first call I receive
Variable names must be unique within a query batch or stored procedure
an error message regarding @colParam
parameter.
So I was then thinking in replacing the add with a set at each call:
db.SetParameterValue(cmd, "colParam", "some value on each call")
But is this safe? I have this feeling...??!?!?!?.
Is it thread safe to have just one command object on which I set the value on each call? What if two users set the value at the same time? What happens then?
This is definitely in the premature optimisation bucket. I am not sure what you think you are optomising.
Your real questions should be is DbCommand thread safe. The answer is no. From the documenation on DbCommand http://msdn.microsoft.com/en-us/library/system.data.common.dbcommand.aspx.
Any instance members are not guaranteed to be thread safe.
精彩评论