Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2006-11-29
3
Medium Priority
?
2,412 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
  • 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Screencast - Getting to Know the Pipeline
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…

772 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