Solved

Database programming with multithreads

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
python sqlite question 11 46
control image tags in a string ? 12 110
how to update exe applicatio from internet ? 6 67
Basic Java Case or If-Else statement... 3 43
Here we come across an interesting topic of coding guidelines while designing automation test scripts. The scope of this article will not be limited to QTP but to an overall extent of using VB Scripting for automation projects. Introduction Now…
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
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…

932 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now