dbPool.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #include "StdAfx.h"
  2. #include "dbpool.h"
  3. #include "Global.h"
  4. //////////////////////////////////////////////////////////////////////////
  5. CODBCPool::CODBCPool()
  6. {
  7. m_nRef = 0;
  8. m_nObjRef = 0;
  9. m_nMaxCount = 0;
  10. m_nMinCount = 0;
  11. m_bNeedExit = FALSE;
  12. m_bNeedStop = FALSE;
  13. m_bNeedConnection = FALSE;
  14. }
  15. CODBCPool::~CODBCPool()
  16. {
  17. DestroyAllDBConnections();
  18. }
  19. DWORD CODBCPool::InitializePool(IN LPCTSTR lpDBSource, IN CONST DWORD &dwDBPort, IN LPCTSTR lpDBAccount, IN LPCTSTR lpPassWord, IN LPCTSTR lpDBName, IN CONST INT &nMinConn /* = 1 */, IN CONST INT &nMaxConn /* = 5 */)
  20. {
  21. if ( !lpDBSource || lpDBSource[0] == _T('\0') || !lpDBName || lpDBName[0] == _T('\0') )
  22. {
  23. return 0;
  24. }
  25. m_strDBSource = lpDBSource;
  26. m_dwDBPort = dwDBPort;
  27. m_strDBAccount = lpDBAccount;
  28. m_strPassWord = lpPassWord;
  29. m_strDBName = lpDBName;
  30. m_nMinCount = nMinConn;
  31. m_nMaxCount = nMaxConn;
  32. return IntiAllConnections();
  33. }
  34. void CODBCPool::ReleasePool()
  35. {
  36. DestroyAllDBConnections();
  37. }
  38. INT CODBCPool::IntiAllConnections()
  39. {
  40. DestroyAllDBConnections();
  41. // 开始按照最小数量开始创建;
  42. int nCount = 0;
  43. CDatabase *pDatabase = NULL;
  44. for (int i = 0; i < m_nMinCount; i++)
  45. {
  46. pDatabase = InitAConnection();
  47. if ( pDatabase )
  48. {
  49. nCount++;
  50. m_listIdleConnections._push_back(pDatabase);
  51. InterlockedIncrement(&m_nObjRef); //增加引用计数;
  52. pDatabase = NULL;
  53. }
  54. }
  55. return nCount;
  56. }
  57. void CODBCPool::DestroyAllDBConnections()
  58. {
  59. m_bNeedExit = TRUE;
  60. // 首先等待m_listBusyConnections.size() == 0;
  61. while(1)
  62. {
  63. if ( m_listBusyConnections.size() == 0 )
  64. break;
  65. Sleep(1000);
  66. }
  67. ODBCConnectList::iterator itIdle = m_listIdleConnections.begin();
  68. while (itIdle != m_listIdleConnections.end())
  69. {
  70. if (NULL != (*itIdle))
  71. {
  72. (*itIdle)->Close();
  73. delete (*itIdle);
  74. }
  75. itIdle = m_listIdleConnections.erase(itIdle);
  76. }
  77. m_bNeedExit = FALSE;
  78. }
  79. CDatabase* CODBCPool::GetAConnection( IN CONST DWORD &dwTimeOut /* = 1000 */ )
  80. {
  81. if ( m_bNeedExit )// 停止获取;
  82. return NULL;
  83. // 1.首先到池中查找有无空闲对象;
  84. BOOL bGetIdl = FALSE;
  85. DWORD dwTime = GetTickCount();
  86. CDatabase* pDBEngine = NULL;
  87. do
  88. {
  89. if ( m_listIdleConnections._size() > 0){
  90. //AutoThreadSection aSection(&m_critSection);
  91. pDBEngine = m_listIdleConnections._pop_front_();
  92. if (pDBEngine)
  93. {
  94. m_listBusyConnections._push_back(pDBEngine);
  95. bGetIdl = TRUE;
  96. }
  97. }
  98. else
  99. {
  100. if (m_nObjRef < m_nMaxCount)
  101. {
  102. //AutoThreadSection aSection(&m_critSection);
  103. pDBEngine = InitAConnection();
  104. if (pDBEngine){
  105. //bGetIdl = TRUE;
  106. m_listBusyConnections._push_back(pDBEngine);
  107. InterlockedIncrement(&m_nObjRef); //增加引用计数;
  108. Global::WriteTextLog(_T("创建连接对象:共有 %d 个"), m_nObjRef);
  109. break;
  110. }
  111. }
  112. }
  113. if ( !bGetIdl ) {
  114. // 未找到,小憩一会,防止CPU爆满;
  115. Sleep(0);
  116. // 超时,则结束返回NULL;
  117. if ( (GetTickCount() - dwTime) >= dwTimeOut){
  118. Global::WriteTextLog(_T("获取连接对象超时"));
  119. break;
  120. }
  121. }
  122. } while ( !bGetIdl );
  123. return pDBEngine;
  124. }
  125. void CODBCPool::RestoreAConnection(IN CDatabase *pDBEngine)
  126. {
  127. if ( pDBEngine != NULL ){
  128. m_listBusyConnections._remove(pDBEngine);
  129. if ( pDBEngine->IsOpen() )
  130. m_listIdleConnections._push_back(pDBEngine);
  131. else
  132. delete pDBEngine;
  133. }
  134. }
  135. CDatabase* CODBCPool::InitAConnection()
  136. {
  137. if ( m_nObjRef == m_nMaxCount )
  138. return NULL;
  139. CDatabase * pDBEngine = new CDatabase;
  140. TCHAR szConnString[MAX_PATH] = {0};
  141. if (m_dwDBPort != 0)
  142. {
  143. if ( m_strDBAccount.IsEmpty() )
  144. _stprintf_s(szConnString, MAX_PATH, DB_SW_CONN_WITH_PORT, m_strDBSource, m_dwDBPort,m_strDBName);
  145. else
  146. _stprintf_s(szConnString, MAX_PATH, DB_SS_CONN_WITH_PORT,m_strDBSource, m_dwDBPort, m_strDBName, m_strDBAccount, m_strPassWord);
  147. }
  148. else
  149. {
  150. if ( m_strDBAccount.IsEmpty() )
  151. _stprintf_s(szConnString, MAX_PATH, DB_SW_CONN_WITHOUT_PORT, m_strDBSource, m_strDBName);
  152. else
  153. _stprintf_s(szConnString, MAX_PATH, DB_SS_CONN_WITHOUT_PORT, m_strDBSource, m_strDBName, m_strDBAccount, m_strPassWord);
  154. }
  155. try
  156. {
  157. pDBEngine->OpenEx(szConnString, CDatabase::noOdbcDialog);
  158. }
  159. catch (CDBException* e)
  160. {
  161. if ( pDBEngine )
  162. delete pDBEngine;
  163. pDBEngine = NULL;
  164. Global::WriteTextLog(_T("SQL连接串:%s, 返回错误:%s"), szConnString, e->m_strError);
  165. }
  166. return pDBEngine;
  167. }
  168. void CODBCPool::CloseAConnection(CDatabase *&pDataBase)
  169. {
  170. m_listIdleConnections._remove(pDataBase);
  171. InterlockedDecrement(&m_nObjRef); // 减少计数;
  172. if ( pDataBase )
  173. delete pDataBase;
  174. pDataBase = NULL;
  175. }
  176. void CODBCPool::CloseBusyConnection(IN CDatabase *&pDataBase)
  177. {
  178. m_listBusyConnections._remove(pDataBase);
  179. InterlockedDecrement(&m_nObjRef); // 减少计数;
  180. if ( pDataBase )
  181. pDataBase->Close();
  182. }