Solved

Database programming with multithreads

Posted on 2004-04-01
2
248 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
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Sql how to exclude from a table 3 73
hasOne  challenge 59 89
array6 challenfge 6 62
array220 challenge 8 45
A short article about a problem I had getting the GPS LocationListener working.
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
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 …
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 …

744 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

18 Experts available now in Live!

Get 1:1 Help Now