Solved

Database Race Condition?

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Excel file not created as expected 7 46
.Net Web Site IIS Web.Config Content-Security-Policy 1 36
Receiving a string from a WebService Push 21 32
Error in script 11 44
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
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…

932 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

12 Experts available now in Live!

Get 1:1 Help Now