P
Peter Lin
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..
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..