Solved

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

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

776 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