Solved

Database Race Condition?

Posted on 2013-11-26
1
249 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

708 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now