Solved

Database programming with multithreads

Posted on 2004-04-01
2
263 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
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

786 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