Results 1 to 2 of 2

Thread: how you would solve this issue?

  1. #1
    Join Date
    Jun 2008
    Posts
    4

    how you would solve this issue?

    1. You have discovered a bug in a multi-threaded windows application method that serves lookup data. The method fills a static list object full of simple data objects. When only one thread hits the method the method executes rapidly. The SQL stored procedure that is returning the results is optimized and runs quickly. You can test it using a T-SQL call and it runs in less than a millisecond. When one thread accesses the method the code returns just as quickly. But when more than one thread hits the method at the same time load up of the static list object becomes slower in an exponential fashion, driving system resource usage up and taking much longer than expected. After the list runs then the usage drops again, since the method is no longer called until the application is recycled.

    private static List<DataObject> _list = null;


    public static List<DataObject> GetList()
    {
    if (_list == null)
    {
    _list = new List<DataObject>();
    SqlDataReader reader = null;
    SqlConnection connection =
    new SqlConnection(ConnectionString);
    SqlCommand command = null;


    // Set up a new command
    command = new SqlCommand("GetList", connection);
    command.CommandType = CommandType.StoredProcedure;
    reader =
    command.ExecuteReader(CommandBehavior.CloseConnect ion);

    while (reader.Read())
    {
    DataObject instance = new DataObject();
    instance.ID = reader["ID"] != DBNull.Value &&
    reader["ID"] != null ? (int)reader["ID"] : 0;
    instance.Name = reader["Name"] != DBNull.Value &&
    reader["Name"] != null ? (string)reader["Name"] :
    string.Empty;
    _list.Add(instance);
    }

    }
    }
    First, give an explanation of what is wrong and why the issue stated above is happening. Then write the code and give a detailed explanation of how you would solve this issue.

  2. #2
    Join Date
    Jun 2004
    Location
    Atlanta and Manhattan
    Posts
    607

    That's Quite an Assignment Question!

    And you seem to have copied it verbatim!

    Best of luck in short-circuiting your schoolwork / exam prep.

    Bill

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •