请教各位高手:在多线程里面能否建立数据库连接
我在主线程里面建立了数据库连接,在每个子线程里面实现数据库操作(PROC),但子线程数目大于2时就会CORE,如果只有一个子线程则运行正常,实在不明白什么道理,我在子线程里面使用了事务控制语句,不知道是否如此使用会造成冲突?各位能否给点建议。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
要在数据库操作前加锁
pthread_mutex_t m_Mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_lock
.....
然后pthread_mutex_unlock
下午研究了一下发现PRO*C对线程有特定的用法,就是为每个线程申请一个特定的上下文,但我在编译时还是遇到了问题,如下:
sql_context ctx1;
EXEC SQL ENABLE THREADS;
EXEC SQL CONTEXT ALLOCATE :ctx1;
第三句话编译不通过,报错如下:
Semantic error at line 378, column 29, file autonextdealfork.pc:
EXEC SQL CONTEXT ALLOCATE :ctx1;
我在编译选项中已经添加了trh\
如下是我的编译命令,已经添加了THREADS=YES的编译选项
proc threads=yes oraca=yes code=cpp dbms=v8 parse=partial sqlcheck=semantics \
userid=test/test@HP01 \
include=/usr/include \
include=/opt/aCC \
include=/opt/aCC/include \
include=/opt/aCC/include/iostream \
include=/opt/aCC/include/SC \
include=/oracle/precomp/public \
include=/oracle/rdbms/demo \
include=../include \
iname=autonextdealfork.pc oname=autonextdealfork.c
/*例子程序thread5.pc*/
#include <stdio.h>;
#include <stdlib.h>;
#include <string.h>;
#include <sqlca.h>;
#include <pthread.h>;
//主函数
int main()
{
sql_context ctx1;
EXEC SQL ENABLE THREADS;
EXEC SQL CONTEXT ALLOCATE :ctx1;
EXEC SQL CONTEXT USE :ctx1;
int i=0;
while(i<10)
{
printf("test %d",i);
i++;
sleep(atoi(sleepseconds));
}
EXEC SQL CONTEXT FREE :ctx1;
}
/*以下为编译语句*/
/*
proc threads=yes oraca=yes code=cpp dbms=v8 parse=partial sqlcheck=semantics \
userid=test/test@hp01 \
include=/usr/include \
include=/opt/aCC \
include=/opt/aCC/include \
include=/opt/aCC/include/iostream \
include=/opt/aCC/include/SC \
include=/oracle/precomp/public \
include=/oracle/rdbms/demo \
include=../include \
iname=thread5.pc oname=thread5.c
*/
以下为编译错误信息:
System default option values taken from: /oracle/precomp/admin/pcscfg.cfg
Semantic error at line 12, column 29, file thread5.pc:
EXEC SQL CONTEXT ALLOCATE :ctx1;
............................1
PCC-S-02322, found undefined identifier
Semantic error at line 12, column 29, file thread5.pc:
EXEC SQL CONTEXT ALLOCATE :ctx1;
............................1
PCC-S-02336, host variable expression has invalid type
Semantic error at line 12, column 29, file thread5.pc:
EXEC SQL CONTEXT ALLOCATE :ctx1;
............................1
PCC-S-02391, Runtime context variable not of correct type
Semantic error at line 23, column 25, file thread5.pc:
EXEC SQL CONTEXT FREE :ctx1;
........................1
PCC-S-02322, found undefined identifier
Semantic error at line 23, column 25, file thread5.pc:
EXEC SQL CONTEXT FREE :ctx1;
........................1
PCC-S-02336, host variable expression has invalid type
Semantic error at line 23, column 25, file thread5.pc:
EXEC SQL CONTEXT FREE :ctx1;
........................1
PCC-S-02391, Runtime context variable not of correct type
试试:
EXEC SQL BEGIN DECALRE SECTION;
sql_context ctx1;
EXEC SQL END DECLARE SECTION;