DBPool.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #include "../pch/pch.h"
  2. #include "DBPool.h"
  3. DBPool::DBPool()
  4. {
  5. }
  6. DBPool::~DBPool()
  7. {
  8. this->DestoryConnPool();
  9. }
  10. void DBPool::InitPool()
  11. {
  12. this->database = CConfigReader::getDBName();
  13. this->user = CConfigReader::getMysqlUsername();
  14. this->password = CConfigReader::getMysqlPassword();
  15. this->url = CConfigReader::getMysqlHost();
  16. this->maxSize = MAX_DB_CONNECTION_NUM;
  17. this->curSize = 0;
  18. try {
  19. this->driver = sql::mysql::get_driver_instance();
  20. }
  21. catch (sql::SQLException& e)
  22. {
  23. LOG_INFO("SQLException:" << e.what());
  24. cout<<"SQLException:" << e.what()<<endl;
  25. }
  26. catch (std::runtime_error& e)
  27. {
  28. LOG_INFO("SQLException:" << e.what());
  29. cout << "SQLException:" << e.what() << endl;
  30. }
  31. this->InitConnection(maxSize / 2);
  32. }
  33. void DBPool::InitConnection(int initSize)
  34. {
  35. Connection* conn;
  36. std::lock_guard<std::mutex> guard(m_mutex);
  37. for (int i = 0; i < initSize; i++)
  38. {
  39. conn = this->CreateConnection();
  40. if (conn)
  41. {
  42. //cout << "connect mysql success" << endl;
  43. connList.push_back(conn);
  44. ++(this->curSize);
  45. }
  46. else
  47. {
  48. LOG_INFO("create conn error");
  49. //cout << "create conn error" << endl;
  50. }
  51. }
  52. }
  53. Connection* DBPool::CreateConnection()
  54. {
  55. Connection* conn;
  56. try {
  57. conn = driver->connect(this->url.c_str(), this->user.c_str(), this->password.c_str()); //create a conn
  58. conn->setSchema(this->database.c_str());
  59. return conn;
  60. }
  61. catch (sql::SQLException& e)
  62. {
  63. LOG_INFO("SQLException:" << e.what());
  64. //cout << "SQLException:" << e.what() << endl;
  65. return NULL;
  66. }
  67. catch (std::runtime_error& e)
  68. {
  69. LOG_INFO("SQLException:" << e.what());
  70. //cout << "SQLException:" << e.what() << endl;
  71. return NULL;
  72. }
  73. }
  74. Connection* DBPool::GetConnection()
  75. {
  76. Connection* conn;
  77. std::lock_guard<std::mutex> guard(m_mutex);
  78. if (connList.size() > 0)//the pool have a conn
  79. {
  80. conn = connList.front();
  81. connList.pop_front();//move the first conn
  82. if (conn->isClosed() || conn->isValid() == false)
  83. {
  84. //如果连接已经不可用,重新建立一个连接
  85. if (conn->isClosed() == false)
  86. {
  87. conn->close();
  88. }
  89. delete conn;
  90. conn = this->CreateConnection();
  91. if (conn == NULL)
  92. {
  93. LOG_INFO("new conn is null!");
  94. --curSize;
  95. }
  96. }
  97. //这里直接返回连接,不能加入池子,等用完了会通过ReleaseConnection加入池子
  98. return conn;
  99. }
  100. else
  101. {
  102. if (curSize < maxSize)//the pool no conn
  103. {
  104. conn = this->CreateConnection();
  105. if (conn)
  106. {
  107. ++curSize;
  108. return conn;
  109. }
  110. else
  111. {
  112. return NULL;
  113. }
  114. }
  115. else //the conn count > maxSize
  116. {
  117. //连接池已经达到最大数量了
  118. return NULL;
  119. }
  120. }
  121. }
  122. //put conn back to pool
  123. void DBPool::ReleaseConnection(Connection *conn)
  124. {
  125. if (conn)
  126. {
  127. std::lock_guard<std::mutex> guard(m_mutex);
  128. connList.push_back(conn);
  129. }
  130. }
  131. void DBPool::DestoryConnPool()
  132. {
  133. list<Connection*>::iterator iter;
  134. std::lock_guard<std::mutex> guard(m_mutex);
  135. for (iter = connList.begin(); iter != connList.end(); ++iter)
  136. {
  137. this->DestoryConnection(*iter);
  138. }
  139. curSize = 0;
  140. connList.clear();
  141. }
  142. void DBPool::DestoryConnection(Connection* conn)
  143. {
  144. if (conn)
  145. {
  146. try {
  147. conn->close();
  148. }
  149. catch (sql::SQLException&e)
  150. {
  151. perror(e.what());
  152. }
  153. catch (std::exception& e)
  154. {
  155. perror(e.what());
  156. }
  157. delete conn;
  158. }
  159. }