Solved

Database Race Condition?

Posted on 2013-11-26
1
253 Views
Last Modified: 2013-12-08
I have a Race condition where each time I run my code, the number of items returned changes and is slightly different for each run but if I step through the code, I see all records as expected.
My gut is that I was trying to make the db call thread same and was using IAsyncResult to accomplish that. I may be way off track here...

In my main program, I am instantiating a class that does some SQL DB work like this:
Database db = new Database();
db.BeginDatabaseCall();

Open in new window


my Database.cs
public void BeginDatabaseCall()
{
	SqlConnection conn = new SqlConnection(strMyConnectionString);
	SqlCommand cmd = new SqlCommand(sql,conn);
	conn.Open();
	cmd.BeginExecuteReader(EndDatabaseCall, cmd);
}
	public void EndDatabaseCall(IAsyncResult result)
{
	SqlCommand cmd = result.AsyncState as SqlCommand;
		try
	{
		using (SqlDataReader reader = cmd.EndExecuteReader(result))
		{
			while (reader.Read())
			{
				aryLO.Add(reader["LO"]);
				aryPR.Add(reader["PR"]);
				aryBLN.Add(reader["BLN"]);
				aryMS.Add(reader["MS"]);
			}
		}
	}
	catch
	{
		throw;
	}
	finally
	{
		cmd.Connection.Close();
		cmd.Connection.Dispose();
		cmd.Dispose();
	}
}

Open in new window


Thanks in advance for any assistance or guidance you can provide on this topic.
0
Comment
Question by:Technodweeb
1 Comment
 
LVL 11

Accepted Solution

by:
Technodweeb earned 0 total points
ID: 39704937
I took the code above and rewrote it to solve the problem...

public void BeginDatabaseCall()
{
	SqlConnection conn = new SqlConnection(connString);
	SqlCommand cmd = new SqlCommand(sql.ToString(), conn);
	conn.Open();
	SqlDataReader reader = cmd.ExecuteReader();

	try
	{
		while (reader.Read())
		{
			aryLO.Add(reader["LO"]);
			// NULL Prevention
			if (string.IsNullOrEmpty(reader["PName"].ToString())) { aryProc.Add("Unassigned"); } else { aryProc.Add(reader["PName"]); }
			aryBLast.Add(reader["BLast"]);
			aryMS.Add(reader["Status"]);
		}
	}
	catch(Exception ex)
	{
		System.Windows.Forms.MessageBox.Show(ex.ToString());
		throw;
	}
	finally
	{
		cmd.Connection.Close();
		cmd.Connection.Dispose();
		cmd.Dispose();
	}
}

Open in new window

0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Hey Audio! 3 30
VB.NET - Error - Object not set in Module 4 33
Store results in vb.net 3 21
Latest thoughts on C# utilizing Mono on Linux vs .NET on Windows? 5 29
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question