请选择 进入手机版 | 继续访问电脑版
查看: 456|回复: 0

[运维管理] 菜鸟用C++实现一轻量级的mysql连接池

688

主题

688

帖子

2083

积分

猿er

Rank: 1

积分
2083
发表于 2016-8-14 17:48:51
<无详细内容>
  1. /*************************************************************************
  2. > File Name: my_Pool.cpp
  3. > Author: LCG
  4. > Mail:
  5. > Created Time: Fri Apr 29 12:33:15 2016
  6. ************************************************************************/
  7. #include<mysql.h>
  8. #include<stdio.h>
  9. #include<string>
  10. #include<string.h>
  11. #include<stdlib.h>
  12. #include<list>
  13. #include<pthread.h>
  14. #include<iostream>
  15. #include "my_Pool.h"
  16. using namespace std;
  17. my_Pool* my_Pool::connPool = NULL;
  18. my_Pool::my_Pool(string IP,int Port,string User,string PassWord,string DBName,unsigned int MaxConn)
  19. {
  20. this->IP = IP;
  21. this->Port = Port;
  22. this->User = User;
  23. this->PassWord = PassWord;
  24. pthread_mutex_lock(&lock);
  25. for(int i = 0; i < MaxConn; i++)
  26. {
  27. MYSQL * con = NULL;
  28. con = mysql_init(NULL);
  29. if(con == NULL)
  30. {
  31. cout<<"Error:"<<mysql_error(con);
  32. exit(1);
  33. }
  34. con = mysql_real_connect(con,IP.c_str(),User.c_str(),PassWord.c_str(),DBName.c_str(),0,NULL,0);
  35. if(con == NULL)
  36. {
  37. cout<<"Error: "<<mysql_error(con);
  38. exit(1);
  39. }
  40. connList.push_back(con);
  41. ++FreeConn;
  42. }
  43. this->MaxConn = MaxConn;
  44. this->UsedConn = 0;
  45. pthread_mutex_unlock(&lock);
  46. }
  47. my_Pool* my_Pool::GetInstance(string IP,int Port,string User,string PassWord,string DBName,unsigned int MaxConn)
  48. {
  49. if(connPool == NULL)
  50. {
  51. connPool = new my_Pool(IP,Port,User,PassWord,DBName,MaxConn);
  52. }
  53. return connPool;
  54. }
  55. MYSQL* my_Pool::GetConnection()
  56. {
  57. MYSQL * con = NULL;
  58. pthread_mutex_lock(&lock);
  59. if(connList.size() > 0)
  60. {
  61. con = connList.front();
  62. connList.pop_front();
  63. --FreeConn;
  64. ++UsedConn;
  65. pthread_mutex_unlock(&lock);
  66. return con;
  67. }
  68. return NULL;
  69. }
  70. bool my_Pool::ReleaseConnection(MYSQL * con)
  71. {
  72. pthread_mutex_lock(&lock);
  73. if(con != NULL)
  74. {
  75. connList.push_back(con);
  76. ++FreeConn;
  77. --UsedConn;
  78. pthread_mutex_unlock(&lock);
  79. return true;
  80. }
  81. return false;
  82. }
  83. void my_Pool::DestoryPool()
  84. {
  85. pthread_mutex_lock(&lock);
  86. if(connList.size() > 0)
  87. {
  88. list<MYSQL *>::iterator it;
  89. for(it = connList.begin(); it != connList.end(); ++it)
  90. {
  91. MYSQL * con = *it;
  92. mysql_close(con);
  93. connList.pop_front();
  94. }
  95. UsedConn = 0;
  96. FreeConn = 0;
  97. connList.clear();
  98. }
  99. }
  100. int my_Pool::GetFreeConn()
  101. {
  102. return this->FreeConn;
  103. }
  104. my_Pool::~my_Pool()
  105. {
  106. DestoryPool();
  107. }
复制代码
  1. /*************************************************************************
  2. > File Name: my_Pool.h
  3. > Author: LCG
  4. > Mail:
  5. > Created Time: Fri Apr 29 11:21:56 2016
  6. ************************************************************************/
  7. #ifndef _MY_POOL_
  8. #define _MY_POOL_
  9. #include<stdio.h>
  10. #include<list>
  11. #include<mysql.h>
  12. #include<error.h>
  13. #include<mysqld_error.h>
  14. #include<string.h>
  15. #include<iostream>
  16. #include<string>
  17. using namespace std;
  18. class my_Pool
  19. {
  20. public:
  21. my_Pool(string IP,int Port,string User,string PassWord,string DataBaseName,unsigned int MaxConn); //
  22. MYSQL * GetConnection(); //get a mysql connection
  23. bool ReleaseConnection(MYSQL* conn); //Release a Connection
  24. void DestoryPool(); //destory all the pool*/
  25. static my_Pool * GetInstance(string IP,int Port,string User,string PWD,string DataName,unsigned int MaxConn);
  26. int GetFreeConn();
  27. public:
  28. ~my_Pool();
  29. private:
  30. unsigned int MaxConn;
  31. unsigned int UsedConn;
  32. unsigned int FreeConn;
  33. private:
  34. pthread_mutex_t lock;
  35. list<MYSQL *> connList;
  36. my_Pool * conn;
  37. MYSQL *Con;
  38. static my_Pool *connPool;
  39. private:
  40. string IP;
  41. string Port;
  42. string User;
  43. string PassWord;
  44. string DataName;
  45. };
  46. #endif
复制代码
  1. /*************************************************************************
  2. > File Name: Test_Pool.cpp
  3. > Author: LCG
  4. > Mail: lichungangg@163.com
  5. > Created Time: Fri Apr 29 16:31:41 2016
  6. ************************************************************************/
  7. #include"my_Pool.h"
  8. #include<mysql.h>
  9. #include<string.h>
  10. #include<stdlib.h>
  11. #include<iostream>
  12. using namespace std;
  13. int main(void)
  14. {
  15. my_Pool *conn = my_Pool::GetInstance("ipaddress",port,"username","password","dbname",10);
  16. if(conn != 0)
  17. {
  18. cout<<"get connection success"<<endl;
  19. }
  20. else
  21. {
  22. cout<<"get error"<<endl;
  23. }
  24. cout<<"conn = "<<conn->GetFreeConn()<<endl;
  25. MYSQL * con = conn->GetConnection();
  26. const char *query = "select * from tablename ";
  27. if(mysql_query(con,query))
  28. {
  29. cout<<"Failed to query data"<<endl;
  30. exit(1);
  31. }
  32. MYSQL_RES *result;
  33. MYSQL_ROW row;
  34. int numField = 0;
  35. result = mysql_use_result(con);
  36. for(int i = 0; i < mysql_field_count(con);++i)
  37. {
  38. row = mysql_fetch_row(result);
  39. if(row <= 0)
  40. {
  41. break;
  42. }
  43. for(int j = 0; j < mysql_num_fields(result);++j)
  44. {
  45. cout<<row[i]<<" ";
  46. }
  47. cout<<endl;
  48. }
  49. mysql_free_result(result);
  50. return 0;
  51. }
复制代码


回复

使用道具 举报