免费注册 查看新帖 |

Chinaunix

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

多进程访问mysql数据库的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-05-16 08:39 |只看该作者 |倒序浏览
现在有多个进程,当它们连接同一个数据库的时候,该怎么做?
比如:是让每个进程都创建数据库连接?
还是多个进程创建一个数据库连接?
哪个方法更好一些?
有没有更好的方法?
另外,多个进程创建一个数据库连接,多进程访问数据库用加锁保护吗

论坛徽章:
324
射手座
日期:2013-08-23 12:04:38射手座
日期:2013-08-23 16:18:12未羊
日期:2013-08-30 14:33:15水瓶座
日期:2013-09-02 16:44:31摩羯座
日期:2013-09-25 09:33:52双子座
日期:2013-09-26 12:21:10金牛座
日期:2013-10-14 09:08:49申猴
日期:2013-10-16 13:09:43子鼠
日期:2013-10-17 23:23:19射手座
日期:2013-10-18 13:00:27金牛座
日期:2013-10-18 15:47:57午马
日期:2013-10-18 21:43:38
2 [报告]
发表于 2011-05-16 09:55 |只看该作者
当然是多个进程每个都创建数据库连接,换成线程也应该这么做。
数据库自己会解决锁的问题

论坛徽章:
0
3 [报告]
发表于 2011-05-16 16:50 |只看该作者
本帖最后由 bestscw 于 2011-05-16 16:53 编辑

楼主,我的一个多进程插入数据库
看下,会开启18个进程,同时插

  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <string.h>
  4. #include <sys/time.h>
  5. #include <unistd.h>
  6. #include <mysql/mysql.h>
  7. #include<sys/types.h>
  8. #include<sys/wait.h>

  9. #define DB_HOST "127.0.0.1"
  10. #define DB_USER "root"
  11. #define DB_PASS ""
  12. #define DB_NAME "test"
  13. #define DB_PORT 3307

  14. int main(int argc, char **argv)
  15. {
  16.         MYSQL mysql;

  17.         unsigned int i;
  18.         int rtn; /*子进程的返回数值*/
  19.         unsigned int process_no;
  20.         unsigned int userId;
  21.         unsigned int type;
  22.         unsigned int itemCate;
  23.         unsigned int itemType;
  24.         unsigned int itemNum;
  25.         unsigned int totalCost;
  26.         unsigned int consumeTime;
  27.         unsigned int leftBalance;
  28.         char itemName[1000] ;
  29.         char sql[20000] ;

  30.     struct timeval time_start,time_end;
  31.     float execute_time;
  32.        
  33.     //srand(time(0));

  34.         for(process_no = 1; process_no < 18; process_no++)
  35.         {
  36.                 if(fork() == 0)
  37.                 {
  38.                     // 开始执行时间
  39.                     gettimeofday(&time_start,NULL);

  40.                         mysql_init(&mysql);

  41.                         if(!mysql_real_connect(&mysql,DB_HOST,DB_USER,DB_PASS,DB_NAME,DB_PORT,NULL,0))
  42.                         {
  43.                                 fprintf(stderr, "Failed to connect to database: Error: %s\n",mysql_error(&mysql));
  44.                         }

  45.                         mysql_query(&mysql,"SET NAMES utf8");

  46.                         for(i = 0; i < 444444; i++)
  47.                         {
  48.                                 sprintf(itemName,"%s","");

  49.                                 userId = rand()%19071388 + 1;
  50.                                 type = rand()%4 + 1;
  51.                                 itemCate = rand()%15 + 1;
  52.                                 itemType = rand()%100 + 1;
  53.                                 itemNum = rand()%100 + 1;
  54.                                 totalCost = rand()%10000 + 1;
  55.                                 consumeTime = rand()%1309120853 + 1;
  56.                                 leftBalance = rand()%10000 + 1;

  57.                                 sprintf(sql,"insert into user_consume1(userId,type,itemCate,itemType,itemNum,totalCost,consumeTime,leftBalance,itemName)values(%d,%d,%d,%d,%d,%d,%d,%d,'')",
  58.                                         userId,type,itemCate,itemType,itemNum,totalCost,consumeTime,leftBalance);

  59.                                 if(mysql_query(&mysql,sql))
  60.                                 {
  61.                                         fprintf(stderr,"query failed:%s",mysql_error(&mysql));
  62.                                 }
  63.                         }

  64.                         mysql_close(&mysql);

  65.                         // 结束执行时间
  66.                         gettimeofday(&time_end,NULL);

  67.                         execute_time = (time_end.tv_sec - time_start.tv_sec) + (time_end.tv_usec - time_start.tv_usec)/1000000;
  68.                         printf("execute time:%f\n",execute_time);

  69.                         exit(0);
  70.                 }
  71.                 else
  72.                 {
  73.                         /* 父进程, 等待子进程结束,并打印子进程的返回值 */
  74.                         //wait ( &rtn );
  75.                         //printf("child process return %d\n",rtn);
  76.                         //return 0;
  77.                         //printf("execute child process return %d\n",process_no);
  78.                 }
  79.         }





  80.         return 0;
  81. }




复制代码
mysql_insert.rar (1.06 KB, 下载次数: 54)
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP