Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Database Race Condition?

Posted on 2013-11-26
1
Medium Priority
?
263 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:Gregory Miller
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 12

Accepted Solution

by:
Gregory Miller 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

610 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