多线程读写数据库问题
开发一个程序,有10个线程,每个线程都调用SwipeCard函数,而SwipeCard需要多次读写数据库,现在总出问题,大家帮忙看看char SwipeCard(CardInfo* cardInfo,int k)
{
if(cardInfo->ResendMark == 1)
{
if( dbCon->checkReSwipeCard(cardInfo->CardNO) )
{
printf("....................重刷操作并且该操作记录已经存入数据库\n");
return S[k]; //返回0,不开门
}
}
bool feedbackValue;
bool feedback;
int f_ConsumerID;
CString consumerNO;
int f_CardType = 0;
dbCon->getConsumerNO( cardInfo->CardNO ,&consumerNO, &f_ConsumerID, &f_CardType);
feedback = communicateWithDT(doorInfo[k].DoorNO,cardInfo->CardNO,consumerNO, cardInfo->ControllerSN, 1, &feedbackValue,dbCon);
//如果向DT上传刷卡记录失败,则返回0,不开门
if(feedback == false)
{
printf(".....................网络通信失败,无法与HP系统通信\n");
logmsg = "网络通信失败,无法与HP系统通信";
WriteLogFile(logmsg,doorInfo[k].DoorNO);
return 0;
}
if(feedbackValue == true) //如果已经通过了HP的验证
{
printf("....................HP系统验证:卡号与机具号合法\n");
}
else
{
printf("....................HP系统验证卡号成功:卡号与机具号非法\n");
return 0;
}
CString picName;
if(iniCameraConnect[k])
{
TakePhoto( &CameraSocket[k], cardInfo->CardNO, doorInfo[k].cameraNO, CameraServerSocketIP, CameraServerSocketPort, &picName);
printf("....................拍照...\n");
}
int ReSwipe = 0;
CString last_f_ControllerSN;
if( dbCon->checkUserValidity(cardInfo->CardNO, cardInfo->ControllerSN )) //验证本地用户是否合法:卡号与门号是否匹配,是否已经超过了有效期
{
//本地验证合法
printf("....................本地验证通过\n");
if(dbCon->IsComing( cardInfo->CardNO , &ReSwipe, &last_f_ControllerSN)&&ReSwipe>3)
{
//如果记录显示该用户已经进去。那么情况有两种:反潜和出门
//验证是否反潜
printf("...................进入\n");
printf("ReSwiped%d\n");
if(f_CardType == 0 && dbCon->checkRecoming(last_f_ControllerSN)==doorInfo[k].DoorType)
{
if( dbCon->saveCardRecord(0,f_ConsumerID, cardInfo->CardNO, cardInfo->ControllerSN, picName, 0) == 0)
{
printf("....................刷卡记录无法存入数据库\n");
logmsg = "刷卡记录无法存入数据库";
WriteLogFile(logmsg,doorInfo[k].DoorNO);
}
printf("....................反潜\n");
logmsg = "反潜行为";
WriteLogFile(logmsg,doorInfo[k].DoorNO);
// printf("....................向HP系统上传反潜警报\n");
communicateWithHP(doorInfo[k].DoorNO,cardInfo->CardNO,consumerNO, cardInfo->ControllerSN, 2, &feedbackValue,dbCon);
return 0;
}
else //刷卡出门操作
{
//将刷卡记录存入数据库
if( dbCon->saveCardRecord(1,f_ConsumerID, cardInfo->CardNO, cardInfo->ControllerSN, picName, ReSwipe) == 0)
{
printf("....................刷卡记录无法存入数据库\n");
logmsg = "刷卡记录无法存入数据库";
WriteLogFile(logmsg,doorInfo[k].DoorNO);
}
else
{
// printf("....................刷卡记录已经存入数据库\n");
}
}
}
else
{
//将刷卡记录存入数据库
if( dbCon->saveCardRecord(2, f_ConsumerID, cardInfo->CardNO, cardInfo->ControllerSN, picName, ReSwipe) == 0)
{
// printf("....................刷卡记录无法存入数据库\n");
// logmsg = "刷卡记录无法存入数据库";
// WriteLogFile(logmsg,doorInfo[k].DoorNO);
}
else
{
printf("....................刷卡记录已经存入数据库\n");
}
}
return 1; //返回0,不开门
}
else
{
//本地验证不合法
printf("....................本地验证失败,此卡没有注册或者已经过了有效期\n");
AlarmInfo alarminfo;
alarminfo.CardNO = cardInfo->CardNO;
strcmp(alarminfo.ControllerSN, cardInfo->ControllerSN);
alarminfo.AlarmType = 0;
((byte*)(&alarminfo.AlarmType))[2] = 0x01;
alarm1(&alarminfo,dbCon, consumerNO, doorInfo[k].DoorNO); //发出警报
return 0;
}
return 0; //返回0,不开门
} --------------------编程问答-------------------- --------------------编程问答-------------------- 是不是出现那种读写竞争的问题,写的时候不能读,读的时候不能写,可以同时好几个一起读,只有所有的读完之后才能写。我没仔细看你代码,不过你可以想下是不是这个问题。
补充:.NET技术 , VC.NET