Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error while DML / DDL operation through multiple threads.

Status
Not open for further replies.

kathyayini

Programmer
Aug 16, 2002
52
IN
I am having a server which will do the database operations. client will connect to server through port. multiple client can connect to server (In server Multithreading is implemented). if one client connects to server then server will do all the DML /DDL operations without any problem but if 2 clients connects to server at the same time then DML /DDL operation will fail (some time i get the error not logged in, some time unable to fetch from so and so table etc)
Each thread will have different connection. i used mutex also but not used CONTEXT. Please let me know where i am going wrong.

THREADING PART IN MAIN PROGRAM :
while(1)
{
sock_id = accept(serverSocket,(struct sockaddr_in*)&sockAddr,&addrLen);
//printf("Keep Running is : %d\n",keep_running);
/* if(keep_running == 0)
{
printf("Signal Recived coming out \n");
break;
}*/
pthread_mutex_lock(&(tCounter.mutex));
threadCount = tCounter.threadCount;
pthread_mutex_unlock(&(tCounter.mutex));

if(sock_id == -1)
{
if(errno == EINTR)
{
printf("Signal Recived coming out \n");
break;
}
else
continue;
}
nextIndex = getFree();

// Pass the sock_id to Thread
newSocket[nextIndex].socket_id = sock_id;
// Pass the segment size to Thread
newSocket[nextIndex].segment_size=segment_size;

/*if(newSocket[nextIndex].socket_id>0)
{
}*/

rc = pthread_create(&cur, NULL, RECEIVEBUFFER, (void*)&newSocket[nextIndex]);

pthread_detach(cur);

pthread_mutex_lock(&(tCounter.mutex));
tCounter.threadCount++;
pthread_mutex_unlock(&(tCounter.mutex));
}


PART OF FUNCTION WHICH WILL BE CALLED THRU THREAD

int rc = Connect2DB((char *)"ml", (char *)"ml");
EXEC SQL UPDATE LOG_TRANSACTION SET STATUS='N' WHERE REF_NO=:cRefNo AND STATUS = 'D';
EXEC SQL COMMIT;
 
you are performing all the function outside mutex
the process is u should lock mutex
perform all ur function and
then unlock the mutex
whereas wht i see is u are locking the mutex checking only if it is busy or not and coming out.
 
you have to see what part of your server data should be thread-safe and lock the mutex before the portion of code where you are accessing that data. unlock mutex only after that part is done.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top