public static void XSafeQuery(AbstractSqlConnectionDescriptor connDesc,
SqlTransaction transaction, string sql,
IEnumerable sqlParams, DataReaderDelegate invokeMe, Hashtable parameters)
{
IDbConnection conn = transaction != null
? transaction.Connection
: DbCaches.Connections.Get(connDesc);
try
{
IDbCommand cmd = DbCaches.Commands.Get(sql, conn);
if (transaction != null)
{
cmd.Transaction = transaction.Transaction;
}
try
{
SetSQLOnCommand(connDesc, cmd, sql, sqlParams);
IDataReader reader;
try
{
reader = cmd.ExecuteReader();
}
catch (Exception e)
{
throw new UnableToRunSqlException(connDesc, sql, sqlParams, e);
}
try
{
if (invokeMe != null)
{
invokeMe(parameters, reader);
}
}
catch (ExceptionWithConnectionInfo)
{
// The delegate may have thrown an exception that came from another
// database utilities call, in which case we don't want to confuse
// the issue by wrapping with a different SQL command.
throw;
}
catch (Exception e)
{
throw new UnableToProcessSqlResultsException(connDesc, sql, sqlParams, e);
}
finally
{
try
{
reader.Close();
}
catch (Exception e)
{
// This exception is not rethrown because we don't want to mask any
// previous exception that might be being thrown out of "invokeMe".
_log.Warn("Caught an exception while trying to close the data reader.", e);
}
}
}
finally
{
DbCaches.Commands.Return(sql, conn, cmd);
}
}
finally
{
if (transaction == null)
{
DbCaches.Connections.Return(connDesc, conn);
}
}
}