- 论坛徽章:
- 0
|
各位大侠,
我在Solaris下编写 ESQL/C for Sybase 的程序,采用多线程方式,在线程里面建立数据库连接,结果只有第一个线程可以成功访问到数据库,第二个线程就读不出来数据。。。。。但也没有报错。。。
请各位帮忙,谢谢!
程序代码如下:
void * sybase_thread( void *arg )
{
int iRet ;
exec sql include sqlca ;
exec sql begin declare section;
int iCount;
char hello_world[28];
char tmp_line_code[1+2];
exec sql end declare section;
/*
exec sql whenever sqlerror goto exit_MulThr ;
exec sql whenever sqlwarning goto exit_MulThr ;
exec sql whenever not found continue ;
*/
/*
** protect acquiring cs_objects resources
*/
memset( tmp_line_code, '\0', sizeof(tmp_line_code) );
strcpy( tmp_line_code, (char * )arg );
cout << "tmp_line_code = " << tmp_line_code << endl;
cout << tmp_line_code << tmp_line_code << endl;
pthread_mutex_lock(&gMutexCode);
exec sql connect "sa" identified by "sybase" using "server";
pthread_mutex_unlock(&gMutexCode);
/*
exec sql select 'Hello Sybase World' into :hello_world;
*/
iCount = 0;
EXEC SQL SELECT count(*) INTO :iCount
FROM station_code
WHERE line_id = '03';
printSQLMsg();
iRet = SQLCODE;
cout << pthread_self() << ", iCount = " << iCount << endl;
// printf("[%d]: %s\n", pthread_self(), hello_world);
/*
exec sql disconnect all ;
*/
return( 0 ) ;
exit_MulThr:
printf("[%d]: Error: %d %s \n", pthread_self(),sqlca.sqlcode,sqlca.sqlerrm.sqlerrmc);
return( 0 );
}
int main()
{
for (i = 0 ; i < 2 ; i++ ) {
pthread_create( &stid[i], NULL, sybase_thread, (void *)"3" );
//pthread_create( &stid[1], NULL, DataInitByLine, (void *)"04" );
}
for (i = 0 ; i < 2 ; i++ )
pthread_join( stid[i], NULL ) ;
return( 0 );
}
输出结果:
tmp_line_code = 3
33
tmp_line_code = 3
33
6, iCount = 0
5, iCount = 90
只有线程号为 5 的线程查询成功了。。。。线程号为 6 的线程没有访问到数据库??
在多台Sun的服务器上试了多次都是这样。。。。
请各位指教!!谢谢了!! |
|