多线程操作数据的问题??
大概有200个线程同时对数据库进行读写,线程锁已经加了,在程序运行一段时间之后,突然会出现异常,进入catch(...)代码段,再过一会程序就直接down掉,
线程里面执行sql的函数如下:
bool CCollectThread::ExecSql(CString strSql)
{
Lock::scoped_lock guard(m_mutex);
_CommandPtr pCommand;
_variant_t vNull;
bool nRlt = true;
try
{
pCommand.CreateInstance(__uuidof(Command));
pCommand->ActiveConnection = m_pConn;
pCommand->CommandType = adCmdText;
pCommand->CommandText = (_bstr_t)strSql;
vNull.vt = VT_ERROR;
vNull.scode = DISP_E_PARAMNOTFOUND;
pCommand->Execute(&vNull, NULL, adCmdUnknown);
}
catch(_com_error &e)
{
CString strError;
_bstr_t bstrDescription(e.Description());
strError.Format("访问数据库发生错误。\r\n错误的描述:%s\r\n执行的sql: %s\r\n",(LPCTSTR)bstrDescription,strSql);
GetSysCurrentTime(m_strTime);
CLogFile::Instance().WriteErrorLog((LPSTR)(LPCTSTR)m_strTime, "CCollectThread::Run()", (LPSTR)(LPCTSTR)strError);
nRlt = false;
}
catch(CMemoryException &e)
{
TCHAR szCause[255];
GetSysCurrentTime(m_strTime);
e.GetErrorMessage(szCause, 255);
CLogFile::Instance().WriteErrorLog((LPSTR)(LPCTSTR)m_strTime, "CCollectThread::Run()", szCause);
return false;
}
catch(...)
{
CString strError;
strError.Format("ExecSql发生错误。\r\n执行的sql: %s\r\n",strSql);
GetSysCurrentTime(m_strTime);
CLogFile::Instance().WriteErrorLog((LPSTR)(LPCTSTR)m_strTime, "CCollectThread::Run()", (LPSTR)(LPCTSTR)strError);
nRlt = false;
}
return nRlt;
}
m_pConn是主进程传入的,之前已经做过“套间”处理,即调用了CoMarshalInterThreadInterfaceInStream和CoGetInterfaceAndReleaseStream
请高手们帮忙分析一下会有什么样的原因导致程序在运行一段时间之后突然崩溃?