Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7574249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:23:26+00:00 2026-05-30T16:23:26+00:00

I’m using the MySQL .Net libraries in an older C# application I’m rewriting. The

  • 0

I’m using the MySQL .Net libraries in an older C# application I’m rewriting. The Data Access Layer is rather obsolete but I’m trying to make the best of it. But now I ran into some really nasty threading issues.

I have a series of about 20 Select statements which are used to process a report. They take about 5 seconds to complete and I’m displaying a progress bar while the Select statements run. I’m launching the operations via a simple ThreadPool call:

[LATER EDIT: What happens is that I called the method below twice due to a bug in my UI – this doesn’t devalue the question though, merely explains why my threads were racing against each other.]

ThreadPool.QueueUserWorkItem(new WaitCallback(UpdateChart));
  • Sometimes it works.
  • Sometimes it crashes with a “possible IO stream race condition”.
  • Sometimes it crashes with “connection should be open and valid”.
  • Sometimes it crashes with “object reference not set…”.

All classes in my DAL are Static because I thought this is a good way of improving performance (not having to create new class instances for every little operation).

And all my DAL classes use the same “root” DAL class which builds Connections:

public static class MySQLConnectionBuilder
{

private static MySqlConnectionStringBuilder ConnectionStringBuilder = new MySqlConnectionStringBuilder();

//I'm initializing the ConnectionStringBuilder with my server password & address.

public static MySqlConnection GetConnection ()
{
  return new MySqlConnection(ConnectionStringBuilder.ConnectionString);
}

}

All my DAL classes have functions which are similar to the crashing function. The crashing function looks like this:

public static STDS.UserPresence.user_presenceDataTable GetPresence (int aUserID, DateTime aStart, DateTime aEnd)
{
  ta.Connection = MySQLConnectionBuilder.GetConnection();
  ds = ta.GetPresenceForUserBetweenDates(aUserID, aStart, aEnd);
  ta.Connection.Close();
  return ds;
}

Ideas? Tips on improvement? Will the threading issue go away if I switch to a more object-oriented (instance-driven) DAL?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T16:23:27+00:00Added an answer on May 30, 2026 at 4:23 pm

    The line

    ta.Connection.Close()
    

    Closes the connection last assigned to ta.Connection – not always the connection created in the same thread. This may close a connection on which a query is currently running in another thread.

    If you want to quickly determine if this is what’s happening, mark the connection variable with a [ThreadStatic] attribute in the class ta points to:

    [ThreadStatic]
    private static MySqlConnection connection;
    

    I wouldn’t use that approach for your final solution though, as it may cause the GC not to collect them.

    A simple solution (for that problem, I can’t determine if your classes have other multithreading issues) is to add the connection as a parameter to each of your DAL methods, allowing you to remove the class global Connection:

    public static STDS.UserPresence.user_presenceDataTable GetPresence (int aUserID, DateTime aStart, DateTime aEnd)
    {
      using (MySqlConnection connection = MySQLConnectionBuilder.GetConnection())
      {
          ds = ta.GetPresenceForUserBetweenDates(connection, aUserID, aStart, aEnd);
          return ds;
      }
    }
    

    Threading issues never simply go away – they require attention. If you are unsure about what’s happening, forget about the slight performance boost (if a query takes 5 seconds, any possible performance gain of using static classes would be below 1% anyway).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.