如标题,
创建数据库连接后,使用该连接对象分别创建两个线程,只是简单的做一个同一个表格的查询,单独一个线程正常运行没问题,但是如果两个线程同时启动的话,总是在执行查询语句:SQLExecDirect(hstmt,"select * from UserTable",SQL_NTS)时相互阻塞掉,即产生相互死锁,结果两条语句都无法返回,真纳闷,多线程查询应该是一个经常使用的,怎么会这样,难道数据库驱动无法支持多线程,我用mysql和access同时作了上述测试,结果都会发生上述问题。但是如果分别创建不同的数据库连接分别用于两个线程中,则上述问题不再存在。请高手指点,谢谢!程序很简单,附于后面DWORD WINAPI ReadDBThread1( LPVOID param)
{
int i=0;
RETCODE retcode;
   SWORD ccol;
HSTMT hstmt1;//为执行SQL语句分配HSTMT句柄。
//  retcode=::SQLAllocStmt(DBConnect1.hdbc,&hstmt1);
retcode = SQLAllocHandle(SQL_HANDLE_STMT, DBConnect1.hdbc, &hstmt1);
 
// hstmt1=DBConnect1.hstmt;
 //调用ODBC API直接执行SQL语句。
    CString stringSQL="select * from "+(CString)ORGANIZATION_TABLE_NAME;
char *  strSQL=stringSQL.GetBuffer(stringSQL.GetLength());
while(1)
{
Sleep(10);
printf("Thread1 : %d hstmt1=%x\n",i,hstmt1); retcode=::SQLExecDirect(hstmt1,
 (UCHAR FAR *)strSQL,
  SQL_NTS);//SQL_SUCCESS
 
printf("Thread1 : %d retcode=%d\n",i,retcode);
i++;
}}
DWORD WINAPI ReadDBThread2( LPVOID param)
{
int i=0;
RETCODE retcode;
   SWORD ccol;
HSTMT hstmt2;//为执行SQL语句分配HSTMT句柄。
//  retcode=::SQLAllocStmt(DBConnect2.hdbc,&hstmt2);
retcode = SQLAllocHandle(SQL_HANDLE_STMT, DBConnect1.hdbc, &hstmt2);
// hstmt2=DBConnect1.hstmt; //调用ODBC API直接执行SQL语句。
    CString stringSQL="select * from "+(CString)ORGANIZATION_TABLE_NAME;
char *  strSQL=stringSQL.GetBuffer(stringSQL.GetLength());
  while(1)
{
if(i==10)
break;
Sleep(10); printf("Thread2 : %d hstmt2=%x\n",i,hstmt2);
retcode=::SQLExecDirect(hstmt2,
 (UCHAR FAR *)strSQL,
  SQL_NTS);
 
printf("Thread2 : %d retcode=%d\n",i,retcode);
i++;
}}