Solved

Database programming with multithreads

Posted on 2004-04-01
2
281 Views
Last Modified: 2010-04-17

I developed an database application using VC++ and Micorsoft Access driver as the ODBC source. There are 2 threads in the application both accessing the same table. The first thread writes data in the table and the second thread reads and deletes data from the table. Here is a snapshot of the code of the second thread:

while (!attackRecSet->IsEOF())
{
      .
      .
      .
      .
      .
      .
      try
      {
            attackRecSet->Delete();
            attackRecSet->MoveNext();
      }                                          //try
      catch(CDBException *e)
      {
            MessageBox(NULL, e->m_strError, "Error", MB_OK);
            e->Delete();
      }                                          //catch            
}                                                //while      

An error message occurs because of the delete function and the thread stops working. the error message is:

" Could not update; currently locked by user 'admin' on machine ISLAM_COMPUTER'. "

and when I press OK the following message occurs for ever (it reoccurs if I press the OK button):

" Invalid cursor position; no keyset defined. "

If I commented the delete function the thread will work perfectly. I searched the MSDN for an explanation but with no use. Could anyone tell where could I find an explanation for this error.

Islam Hegazy
0
Comment
Question by:islheg
[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 Comments
 
LVL 1

Accepted Solution

by:
nchristy0 earned 20 total points
ID: 10735664
you need to make sure that one process waits while the other one has access to the database, this is a typical concept in operating systems...

you need to use a semaphore

try this

(this is pseudocode)

prior to accessing the database call P(S), after leaving call V(S)
class Semaphore() {
private value = 0;
private queue of waitting processes;
public function P()
public function V()
}
consutructor() {
value = 0;
}

P() {
 value--;
 if(value < 0) {
          // add process to queue of waitting processes
         // block the process
  }
}

V() {
  value++;
  if(value <= 0) {
     // get 1 process from queue of waitting processes and unblock the process
  }
}

this is the most well known solution to your problem

hope it helps
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Select specific duplicate row based on specific criteria 4 83
Survey branching tutorial 11 76
Eclipse neon2 "Java build path" correctness 7 43
Cannot locate cell 15 41
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

734 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