?
Solved

Database programming with multithreads

Posted on 2004-04-01
2
Medium Priority
?
289 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 40 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

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 show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Introduction to Processes

777 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