DBConnect.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #include <Windows.h>
  2. #include "DBConnect.h"
  3. #include <assert.h>
  4. const uint64_t MYSQL_TIMEOUT = 6 * 3600 * 1000;
  5. CDBConnectPool& CDBConnectPool::GetInstance()
  6. {
  7. static CDBConnectPool db;
  8. return db;
  9. }
  10. void CDBConnectPool::initPool(std::string url_, std::string db_, std::string user_, std::string password_, int maxSize_)
  11. {
  12. User = user_;
  13. Password = password_;
  14. url = url_;
  15. db = db_;
  16. MaxSize = maxSize_;
  17. CurSize = 0;
  18. SqlDriver = sql::mysql::get_driver_instance();
  19. InitConnection(MaxSize >> 1);
  20. }
  21. void CDBConnectPool::InitConnection(int32_t initSize)
  22. {
  23. Head = nullptr;
  24. std::lock_guard<std::mutex> slock(DbLock);//.Enter();
  25. for (int32_t i = 0; i < initSize; i++)
  26. {
  27. CConnectionPtr<sql::Connection>* conn = CreateConnection();
  28. conn->m_Next = Head;
  29. Head = conn;
  30. ++CurSize;
  31. }
  32. //Lock.Leave();
  33. }
  34. CConnectionPtr<sql::Connection>* CDBConnectPool::CreateConnection()
  35. {
  36. try {
  37. sql::Connection* conn = SqlDriver->connect(url.c_str(), User.c_str(), Password.c_str());
  38. conn->setSchema(db.c_str());
  39. uint64_t tick = GetTickCount64();
  40. return new CConnectionPtr<sql::Connection>(conn, tick);
  41. }
  42. catch (sql::SQLException& e)
  43. {
  44. printf("%s", e.what());
  45. return nullptr;
  46. }
  47. return nullptr;
  48. }
  49. CQPtr<CConnectionPtr<sql::Connection>> CDBConnectPool::QueryConnect()
  50. {
  51. CConnectionPtr<sql::Connection>* ptr = nullptr;
  52. std::lock_guard<std::mutex> slock(DbLock);
  53. ULONGLONG CurTick = GetTickCount64();
  54. while (Head != nullptr)
  55. {
  56. if ((CurTick - Head->OperationTick >= MYSQL_TIMEOUT) || !(*Head)->isValid())
  57. {
  58. CConnectionPtr<sql::Connection>* temp = Head;
  59. Head = Head->m_Next;
  60. DestroyConnection(temp);
  61. --CurSize;
  62. }
  63. else
  64. {
  65. ptr = Head;
  66. ptr->SetOperationTick(CurTick);
  67. Head = Head->m_Next;
  68. --CurSize;
  69. break;
  70. }
  71. }
  72. if (ptr == nullptr)
  73. {
  74. if (CurSize < MaxSize)
  75. {
  76. ptr = CreateConnection();
  77. if (ptr != nullptr)
  78. {
  79. ++CurSize;
  80. }
  81. }
  82. else
  83. {
  84. assert(false);
  85. }
  86. }
  87. return ptr;
  88. }
  89. void CDBConnectPool::ReleaseConnection(CConnectionPtr<sql::Connection>* ptr)
  90. {
  91. std::lock_guard<std::mutex> slock(DbLock);
  92. ptr->m_Next = Head;
  93. Head = ptr;
  94. ++CurSize;
  95. }
  96. void CDBConnectPool::DestoryConnPool()
  97. {
  98. std::lock_guard<std::mutex> slock(DbLock);
  99. while (Head != nullptr)
  100. {
  101. CConnectionPtr < sql::Connection>* temp = Head;
  102. Head = Head->m_Next;
  103. DestroyConnection(temp);
  104. }
  105. }
  106. void CDBConnectPool::DestroyConnection(CConnectionPtr<sql::Connection>* conn)
  107. {
  108. (*conn)->close();
  109. delete conn;
  110. }