[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 695
  • Last Modified:

MS SQL Server, multiple threads reading the same table

I want to have two threads reading from the same table with different queries. I use ADO with C++.

Will this work? Any special magic required?
0
Z-Nerd
Asked:
Z-Nerd
  • 2
2 Solutions
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
no magic required, this shall work.

of course, if you try to read (or update) the same records, you will get some "locks" (so your code will get into wait), but for the rest, nothing
0
 
Z-NerdAuthor Commented:
Why would two reads on the same record cause a lock? I understand if updating.
0
 
bclevlndCommented:
You should use hints in your queries if you want sql to not lock records when selecting data.  That is the default behavior of sql.
Use:

select yourselection from yourtable with(nolock)

if you want no data locked.  You will, however, also be returned any dirty records.  You can use

with(rowlock)

to only lock selected rows, or

with(readpast)

to not return any dirty rows.
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
>Why would two reads on the same record cause a lock?

it depends on which transactional level the 2 sessions are.
if the 2 are in serializable mode, everything they touch in common will make the other one to wait on that same resource... because further down the transaction, a update might occur etc...

for single queries, there is no issue, though, as you say, they just will issue a read shared lock, and can both continue without actually blocking each other
0

Featured Post

Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now