免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2560 | 回复: 6
打印 上一主题 下一主题

求助:linux下如何使用多线程操作数据库 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-07-25 17:10 |只看该作者 |倒序浏览
语言是C++。我原来的想法是在每个线程中操作数据库,但现在在每个线程中仅连接数据库都会出现错误。哪位前辈能不能指点一二,如果有这方面的程序示例能否让我拜读、学习?不胜感激!

论坛徽章:
0
2 [报告]
发表于 2006-07-25 18:34 |只看该作者

回复 1楼 starsea3 的帖子

>>
>>
>>你用的什么数据库?多线程是怎么形成的?
>>
>>

论坛徽章:
0
3 [报告]
发表于 2006-07-26 09:22 |只看该作者

RE:Bayweb

数据库是oralce8i;使用pthread_create创建线程;操作数据库的工具是一个已写好的类,这个类封装了对数据库的操作;通过一个该类的指针对数据库进行连接,插入删除等操作.
我现在在线程里new一个这个类的指针,但连接数据库就会出错.

论坛徽章:
0
4 [报告]
发表于 2006-07-26 12:09 |只看该作者
原帖由 starsea3 于 2006-7-26 09:22 发表
数据库是oralce8i;使用pthread_create创建线程;操作数据库的工具是一个已写好的类,这个类封装了对数据库的操作;通过一个该类的指针对数据库进行连接,插入删除等操作.
我现在在线程里new一个这个类的指针,但连接数 ...

>>
>>
>>我以前做过多进程操作数据库的,多线程的没做过。不过应该都支持的。
>>你可以把你的操作数据库相关的代码贴出来看看吗?或者把你的错误信息
>>贴出来看看。
>>
>>
sharkhuang 该用户已被删除
5 [报告]
发表于 2006-07-26 13:27 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
6 [报告]
发表于 2006-07-26 14:19 |只看该作者
我现在在主程序创建连接字段,传入线程处理函数,但仅查询就会出错。代码如下(DBHandlerImpl是一个封装数据库操作的类,太长,就不贴在这里了,是.pc文件,通过proc预编译为cpp文件):
#include "DBHandlerImpl.h"

#include <cstdlib>
#include <iostream>
#include <string>
#include <pthread.h>

using namespace std;

void* threadHandle0(void* pArg);
void* threadHandle1(void* pArg);

int main(void)
{
                DBHandlerImpl* pDBHandler[2];
        pthread_t id[2];
        pDBHandler[0] = new DBHandlerImpl();
        pDBHandler[0]->connect("avs/ora142@37");
        pDBHandler[1] = new DBHandlerImpl();
        pDBHandler[1]->connect("avs/ora142@37");
        pthread_create(&id[0], NULL, threadHandle0,(void*)pDBHandler[0]);
        pthread_create(&id[1], NULL, threadHandle1,(void*)pDBHandler[1]);
        pthread_join(id[0], NULL);
        pthread_join(id[1], NULL);
       
        pDBHandler[0]->close();
        delete pDBHandler[0];
        pDBHandler[1]->close();
        delete pDBHandler[1];
       
        return 0;
}

void* threadHandle0(void* pArg)
{
        cout<<"in the thread 0"<<endl;
       
        DBHandlerImpl* pDBHandlerImpl = (DBHandlerImpl*)pArg;
        string strSql = "select * from A1";
        Recordset rs;
        int nRet = pDBHandlerImpl->execute(strSql.c_str(), &rs, SQL_SELECT, true);
        cout<<"查询结果:"<<nRet<<endl;       
}

void* threadHandle1(void* pArg)
{
        cout<<"in the thread 1"<<endl;
       
        DBHandlerImpl* pDBHandlerImpl = (DBHandlerImpl*)pArg;
        string strSql = "select * from A2";
        Recordset rs;
        int nRet = pDBHandlerImpl->select(strSql.c_str(), &rs);
        cout<<"查询结果:"<<nRet<<endl;       
}

执行结果:
in the thread 0
in the thread 1
Segmentation Fault(coredump)

是不是不能有同一个进程中的两个线程同时操作数据库呀?

[ 本帖最后由 starsea3 于 2006-7-26 14:22 编辑 ]

论坛徽章:
0
7 [报告]
发表于 2006-07-26 22:13 |只看该作者
Segmentation Fault(coredump)

内存溢出了
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP