Solved

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

Posted on 2006-11-29
3
2,246 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 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

830 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