469,595 Members | 2,236 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,595 developers. It's quick & easy.

Are SqlHelper static methods in Microsoft.ApplicationBlocks.Data not thread saft?

Dear all,
I am using Microsoft.ApplicationBlocks.Data, for one thing I am not quite sure.
Is the following static SqlHelper ExecuteReader method from source codes not thread saft in
multithread environment?
Do we need to use lock or monitor to protect our data in the static method
in multithread environment?
Do the following codes have the reentry problem in multithread environment because
I have threads that use SqlHelper to get data from MSSQL?

public static SqlDataReader ExecuteReader(SqlConnection connection,
SqlTransaction transaction, CommandType commandType, string commandText, SqlParameter[]
commandParameters, SqlConnectionOwnership connectionOwnership)
{
if( connection == null ) throw new ArgumentNullException( "connection" );
bool mustCloseConnection = false;
// Create a command and prepare it for execution
SqlCommand cmd = new SqlCommand();
try
{
PrepareCommand(cmd, connection, transaction, commandType, commandText, commandParameters,
out mustCloseConnection );
// Create a reader
SqlDataReader dataReader;
// Call ExecuteReader with the appropriate CommandBehavior
if (connectionOwnership == SqlConnectionOwnership.External)
{
dataReader = cmd.ExecuteReader();
}
else
{
dataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection) ;
}
// Detach the SqlParameters from the command object, so they can be used again.
// HACK: There is a problem here, the output parameter values are fletched
// when the reader is closed, so if the parameters are detached from the command
// then the SqlReader can愒 set its values.
// When this happen, the parameters can愒 be used again in other command.
bool canClear = true;
foreach(SqlParameter commandParameter in cmd.Parameters)
{
if (commandParameter.Direction != ParameterDirection.Input)
canClear = false;
}

if (canClear)
{
cmd.Parameters.Clear();
}
return dataReader;
}
catch
{
if( mustCloseConnection )
connection.Close();
throw;
}
}
Thanks..

Nov 16 '05 #1
0 1445

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

18 posts views Thread by Frank Rizzo | last post: by
1 post views Thread by Ryan Liu | last post: by
reply views Thread by Rajesh Kumar | last post: by
reply views Thread by Rajesh Madhra | last post: by
reply views Thread by Aamir79 | last post: by
3 posts views Thread by =?Utf-8?B?Umljaw==?= | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.