Solved

Multi-thread access to a single datareader in C#.net 2.0

Posted on 2006-11-29
3
2,282 Views
Last Modified: 2008-01-09
I am trying to figure out a way to access a single datareader object from multiple threads.  I create the datareader and pass it into multiple instances of a thread that read from the datareader.  The goal is to have two or more threads reading from the datareader so that thread 1 reads half the records and thread 2 reads the other half.  I can get the threads to not throw errors by messing with the isolation type of the transaction for the connection but only one threads seems to read anything from the datareader and even then it reads only a fraction of the records.  Any ideas on how to accomplish this?
0
Comment
Question by:CBeach1980
[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
  • 2
3 Comments
 
LVL 7

Expert Comment

by:Dimandja
ID: 18038259
To successfully point multiple threads to a STATIC datareader, you must make the datareader thread safe by serializing calls to it.  This in fact defeats the purpose of having multiple threads.

The best thing to do is have each thread create its own datareader.  Each thread can issue queries that it can handle exclusively.
0
 
LVL 4

Author Comment

by:CBeach1980
ID: 18038460
The problem with each creating its own datareader is that I have an enormous dataset (>13 million rows) and would rather not parse the data into smaller sets simply because selecting a subset will take longer because it has to query the data.  If you serialize calls to the datareader how exactly does that impact the datareader and the threads?  I'm not really familiar with serializing calls.
0
 
LVL 7

Accepted Solution

by:
Dimandja earned 500 total points
ID: 18038776
You will have only one thread call to the datareader at a time.  Therefore, you really don't gain anything by using multiple threads, because the threads will be waiting on each other.

Depending on your database organization, you should be able to issue queries that use INDEXES.  This limits how much data is accessed per query.  This also could make it possible to issue queries that return limited and separate data sets.  For example, querying for rows where the index starts with the letter "A" will only return those records, while the next thread may want to return the "B"s, and so on.

Building indexes insures that your whole database is not searched, if you base your query on the index.  It's all about planning.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Code works but breaks when I add one section 4 35
parse string in c# 5 41
C# Windows Form Navigation - Total Beginner 9 94
c#, datatable, aspx 4 53
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

751 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