WxAdoPool.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. #include "StdAfx.h"
  2. #include "WxAdoPool.h"
  3. #include "Global.h"
  4. //////////////////////////////////////////////////////////////////////////
  5. CWxAdoPool::CWxAdoPool()
  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. CWxAdoPool::~CWxAdoPool()
  16. {
  17. DestroyAllDBConnections();
  18. }
  19. DWORD CWxAdoPool::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 CWxAdoPool::ReleasePool()
  35. {
  36. DestroyAllDBConnections();
  37. }
  38. INT CWxAdoPool::IntiAllConnections()
  39. {
  40. DestroyAllDBConnections();
  41. #if USE_ODBC
  42. // 开始按照最小数量开始创建;
  43. int nCount = 0;
  44. CDatabase *pDatabase = NULL;
  45. for (int i = 0; i < m_nMinCount; i++)
  46. {
  47. pDatabase = InitAConnection();
  48. if ( pDatabase )
  49. {
  50. nCount++;
  51. m_listIdleConnections._push_back(pDatabase);
  52. InterlockedIncrement(&m_nObjRef); //增加引用计数;
  53. pDatabase = NULL;
  54. }
  55. }
  56. #else
  57. int nCount = 0;
  58. pAdoObj pDatabase = NULL;
  59. for ( int i = 0; i < m_nMinCount; i++)
  60. {
  61. pDatabase = InitAConnection();
  62. if (pDatabase)
  63. {
  64. nCount++;
  65. m_listIdleConnections._push_back(pDatabase);
  66. InterlockedIncrement(&m_nObjRef);
  67. pDatabase = NULL;
  68. }
  69. }
  70. #endif
  71. return nCount;
  72. }
  73. void CWxAdoPool::DestroyAllDBConnections()
  74. {
  75. m_bNeedExit = TRUE;
  76. // 首先等待m_listBusyConnections.size() == 0;
  77. while(1)
  78. {
  79. if ( m_listBusyConnections.size() == 0 )
  80. break;
  81. Sleep(1000);
  82. }
  83. AdoConnList::iterator itIdle = m_listIdleConnections.begin();
  84. while (itIdle != m_listIdleConnections.end())
  85. {
  86. if (NULL != (*itIdle))
  87. {
  88. (*itIdle)->pConnection->Close();
  89. delete (*itIdle);
  90. }
  91. itIdle = m_listIdleConnections.erase(itIdle);
  92. }
  93. m_bNeedExit = FALSE;
  94. }
  95. pAdoObj CWxAdoPool::GetAConnection( IN CONST DWORD &dwTimeOut /* = 1000 */ )
  96. {
  97. if ( m_bNeedExit )// 停止获取;
  98. return NULL;
  99. // 1.首先到池中查找有无空闲对象;
  100. BOOL bGetIdl = FALSE;
  101. DWORD dwTime = GetTickCount();
  102. pAdoObj pConnection = NULL;
  103. do
  104. {
  105. if ( m_listIdleConnections._size() > 0){
  106. //AutoThreadSection aSection(&m_critSection);
  107. pConnection = m_listIdleConnections._pop_front_();
  108. if (pConnection)
  109. {
  110. m_listBusyConnections._push_back(pConnection);
  111. bGetIdl = TRUE;
  112. }
  113. }
  114. else
  115. {
  116. if (m_nObjRef < m_nMaxCount)
  117. {
  118. //AutoThreadSection aSection(&m_critSection);
  119. pConnection = InitAConnection();
  120. if (pConnection){
  121. //bGetIdl = TRUE;
  122. m_listBusyConnections._push_back(pConnection);
  123. InterlockedIncrement(&m_nObjRef); //增加引用计数;
  124. Global::WriteTextLog(_T("创建连接对象:共有 %d 个"), m_nObjRef);
  125. break;
  126. }
  127. }
  128. }
  129. if ( !bGetIdl ) {
  130. // 未找到,小憩一会,防止CPU爆满;
  131. Sleep(0);
  132. // 超时,则结束返回NULL;
  133. if ( (GetTickCount() - dwTime) >= dwTimeOut){
  134. Global::WriteTextLog(_T("获取连接对象超时"));
  135. break;
  136. }
  137. }
  138. } while ( !bGetIdl );
  139. return pConnection;
  140. }
  141. void CWxAdoPool::RestoreAConnection(IN pAdoObj pObj)
  142. {
  143. if (pObj != NULL ){
  144. m_listBusyConnections._remove(pObj);
  145. if (pObj->pConnection->GetState() != adStateClosed)
  146. m_listIdleConnections._push_back(pObj);
  147. else
  148. delete pObj;
  149. }
  150. }
  151. pAdoObj CWxAdoPool::InitAConnection()
  152. {
  153. if ( m_nObjRef == m_nMaxCount )
  154. return NULL;
  155. pAdoObj pObj = new AdoObj;
  156. pObj->pConnection.CreateInstance(__uuidof(Connection));
  157. TCHAR szConnString[MAX_PATH] = {0};
  158. if (m_dwDBPort != 0)
  159. {
  160. if ( m_strDBAccount.IsEmpty() )
  161. _stprintf_s(szConnString, MAX_PATH, DB_SW_CONN_WITH_PORT, m_strDBSource, m_dwDBPort,m_strDBName);
  162. else
  163. _stprintf_s(szConnString, MAX_PATH, DB_SS_CONN_WITH_PORT,m_strDBSource, m_dwDBPort, m_strDBName, m_strDBAccount, m_strPassWord);
  164. }
  165. else
  166. {
  167. if ( m_strDBAccount.IsEmpty() )
  168. _stprintf_s(szConnString, MAX_PATH, DB_SW_CONN_WITHOUT_PORT, m_strDBSource, m_strDBName);
  169. else
  170. _stprintf_s(szConnString, MAX_PATH, DB_SS_CONN_WITHOUT_PORT, m_strDBSource, m_strDBName, m_strDBAccount, m_strPassWord);
  171. }
  172. try
  173. {
  174. HRESULT hr = pObj->pConnection->Open(_bstr_t(szConnString), "", "", adModeUnknown);
  175. if (FAILED(hr))
  176. {
  177. if (pObj)
  178. delete pObj;
  179. pObj = NULL;
  180. return NULL;
  181. }
  182. pObj->pCommand.CreateInstance(__uuidof(Command));
  183. // 将库连接赋于它;
  184. pObj->pCommand->ActiveConnection = pObj->pConnection;
  185. }
  186. catch (_com_error &e)
  187. {
  188. _bstr_t bstrSource(e.Source());
  189. _bstr_t bstrDescription(e.Description());
  190. if (pObj)
  191. delete pObj;
  192. pObj = NULL;
  193. Global::WriteTextLog(_T("SQL连接串:%08lx,%s,%s,%s"), e.Error(), e.ErrorMessage(), (TCHAR*)bstrSource, (TCHAR*)bstrDescription);
  194. }
  195. return pObj;
  196. }
  197. void CWxAdoPool::CloseAConnection(pAdoObj &pDataBase)
  198. {
  199. m_listIdleConnections._remove(pDataBase);
  200. InterlockedDecrement(&m_nObjRef); // 减少计数;
  201. if ( pDataBase )
  202. delete pDataBase;
  203. pDataBase = NULL;
  204. }
  205. void CWxAdoPool::CloseBusyConnection(IN pAdoObj &pDataBase)
  206. {
  207. m_listBusyConnections._remove(pDataBase);
  208. InterlockedDecrement(&m_nObjRef); // 减少计数;
  209. if ( pDataBase )
  210. pDataBase->pConnection->Close();
  211. }