ODBCPool.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. #include "StdAfx.h"
  2. #include "ODBCPool.h"
  3. CODBCPool* CODBCPool::m_pInstance = NULL;
  4. CODBCPool::CODBCPool(void)
  5. {
  6. m_nRef = 0;
  7. m_nObjRef = 0;
  8. m_nMaxCount = 0;
  9. m_nMinCount = 0;
  10. m_bNeedExit = FALSE;
  11. m_bNeedStop = FALSE;
  12. m_bNeedConnection = FALSE;
  13. m_hWorkThread = NULL;
  14. // 线程控制;
  15. m_hHaveData = CreateEvent (NULL, TRUE, FALSE, _T("DataConnPool"));
  16. //InitializeCriticalSection(&m_csConnList);
  17. InitializeCriticalSection(&m_csIdleConnList);
  18. InitializeCriticalSection(&m_csBusyConnList);
  19. }
  20. CODBCPool::~CODBCPool(void)
  21. {
  22. DestroyAllDBConnections();
  23. if ( m_hHaveData )
  24. SetEvent( m_hHaveData );
  25. // 等待退出;
  26. if ( m_hWorkThread )
  27. {
  28. WaitForSingleObject(m_hWorkThread, INFINITE);
  29. CloseHandle(m_hWorkThread);
  30. }
  31. if ( m_hHaveData )
  32. CloseHandle(m_hHaveData);
  33. DeleteCriticalSection(&m_csIdleConnList);
  34. DeleteCriticalSection(&m_csBusyConnList);
  35. }
  36. CODBCPool* CODBCPool::GetInstance()
  37. {
  38. if ( m_pInstance == NULL )
  39. {
  40. m_pInstance = new CODBCPool;
  41. }
  42. return m_pInstance;
  43. }
  44. // 返回成功初始化的数据库对象数量;
  45. 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 */)
  46. {
  47. if ( !lpDBSource || lpDBSource[0] == _T('\0') ||
  48. !lpDBAccount || lpDBAccount[0] == _T('\0') ||
  49. !lpPassWord || lpPassWord[0] == _T('\0') ||
  50. !lpDBName || lpDBName[0] == _T('\0') )
  51. {
  52. return 0;
  53. }
  54. m_lpDBSource = lpDBSource;
  55. m_dwDBPort = dwDBPort;
  56. m_lpDBAccount = lpDBAccount;
  57. m_lpPassWord = lpPassWord;
  58. m_lpDBName = lpDBName;
  59. m_nMinCount = nMinConn;
  60. m_nMaxCount = nMaxConn;
  61. return IntiAllConnections();
  62. }
  63. INT CODBCPool::IntiAllConnections()
  64. {
  65. // 先七公里现有的数据;
  66. DestroyAllDBConnections();
  67. // 开始按照最小数量开始创建;
  68. int nCount = 0;
  69. for (int i = 0; i < m_nMinCount; i++)
  70. {
  71. nCount = InitAConnection();
  72. }
  73. // 创建一个工作线程,用来进行一些后台维护工作;
  74. //if (NULL == m_hWorkThread)
  75. //{
  76. // m_hWorkThread = CreateThread(NULL, NULL, WorkThread, (LPVOID)this, 0, NULL);
  77. //}
  78. return nCount;
  79. }
  80. void CODBCPool::DestroyAllDBConnections()
  81. {
  82. m_bNeedExit = TRUE;
  83. StopWorkThread();
  84. // 首先等待m_listBusyConnections.size() == 0;
  85. while(1)
  86. {
  87. if ( m_listBusyConnections.size() == 0 )
  88. break;
  89. Sleep(1000);
  90. }
  91. ODBCConnectList::iterator itIdle = m_listIdleConnections.begin();
  92. while (itIdle != m_listIdleConnections.end())
  93. {
  94. if (NULL != (*itIdle))
  95. {
  96. (*itIdle)->Close();
  97. delete (*itIdle);
  98. //#ifdef _DEBUG
  99. static int ncount = 0;
  100. printf("当前删除连接个数:【%d】\t\n",ncount++);
  101. //#endif
  102. }
  103. itIdle = m_listIdleConnections.erase(itIdle);
  104. }
  105. m_bNeedExit = FALSE;
  106. }
  107. int CODBCPool::InitAConnection()
  108. {
  109. EnterCriticalSection(&m_csIdleConnList);
  110. if ( m_nObjRef == m_nMaxCount )
  111. {
  112. LeaveCriticalSection(&m_csIdleConnList);
  113. return m_nMaxCount;
  114. }
  115. BOOL bSuccess = FALSE;
  116. CODBCConnect * pDBEngine = new CODBCConnect(m_lpDBSource, m_dwDBPort, m_lpDBAccount, m_lpPassWord, m_lpDBName, bSuccess);
  117. if (bSuccess)
  118. {
  119. InterlockedIncrement(&m_nObjRef); //增加引用计数;
  120. pDBEngine->SetObjId(m_nObjRef);
  121. m_listIdleConnections.push_back(pDBEngine);
  122. //#ifdef _DEBUG
  123. printf("当前连接池个数:%d\t\n",m_nObjRef);
  124. //#endif
  125. }
  126. else
  127. {
  128. if ( pDBEngine )
  129. delete pDBEngine;
  130. }
  131. LeaveCriticalSection(&m_csIdleConnList);
  132. return m_nObjRef;
  133. }
  134. void CODBCPool::CloseAConnection(CODBCConnect* pDBEngine)
  135. {
  136. // 从空闲队列将其删除;
  137. EnterCriticalSection(&m_csIdleConnList);
  138. m_listIdleConnections.remove(pDBEngine);
  139. InterlockedDecrement(&m_nObjRef); // 减少计数;
  140. LeaveCriticalSection(&m_csIdleConnList);
  141. //pDBEngine->Close();
  142. if ( pDBEngine )
  143. delete pDBEngine;
  144. }
  145. CODBCConnect * CODBCPool::GetAConnection( IN CONST DWORD &dwTimeOut )
  146. {
  147. EnterCriticalSection(&m_csIdleConnList);
  148. if ( m_bNeedExit )
  149. {// 停止获取;
  150. LeaveCriticalSection(&m_csIdleConnList);
  151. return NULL;
  152. }
  153. // 1.首先到池中查找有无空闲对象;
  154. BOOL bGetIdl = FALSE;
  155. DWORD dwTime = GetTickCount();
  156. CODBCConnect* pDBEngine = NULL;
  157. do
  158. {
  159. if ( m_listIdleConnections.size() > 1)
  160. {
  161. pDBEngine = m_listIdleConnections.front();
  162. m_listIdleConnections.pop_front();
  163. EnterCriticalSection(&m_csBusyConnList); // 防止同时push和pop;
  164. m_listBusyConnections.push_back(pDBEngine);
  165. LeaveCriticalSection(&m_csBusyConnList);
  166. bGetIdl = TRUE;
  167. }
  168. else
  169. {
  170. if ( (m_listBusyConnections.size() >= m_nMinCount - 1) && (m_nObjRef < m_nMaxCount))
  171. {
  172. BOOL bSuccess = FALSE;
  173. pDBEngine = new CODBCConnect(m_lpDBSource, m_dwDBPort, m_lpDBAccount, m_lpPassWord, m_lpDBName, bSuccess);
  174. if (bSuccess)
  175. {
  176. InterlockedIncrement(&m_nObjRef); //增加引用计数;
  177. EnterCriticalSection(&m_csBusyConnList); // 防止同时push和pop;
  178. m_listBusyConnections.push_back(pDBEngine);
  179. pDBEngine->SetObjId(m_nObjRef);
  180. LeaveCriticalSection(&m_csBusyConnList);
  181. //#ifdef _DEBUG
  182. printf("当前连接池个数:%d\t\n",m_nObjRef);
  183. //#endif
  184. break;
  185. }
  186. else
  187. {
  188. if ( pDBEngine )
  189. delete pDBEngine;
  190. }
  191. }
  192. }
  193. if ( !bGetIdl )
  194. {
  195. // 未找到,小憩一会,防止CPU爆满;
  196. Sleep(100);
  197. // 超时,则结束返回NULL;
  198. if ( (GetTickCount() - dwTime) >= dwTimeOut)
  199. {
  200. //#ifdef _DEBUG
  201. printf("----------------###【获取连接对象超时】###----------------\n");
  202. //#endif
  203. break;
  204. }
  205. }
  206. } while ( !bGetIdl );
  207. LeaveCriticalSection(&m_csIdleConnList);
  208. return pDBEngine;
  209. }
  210. int CODBCPool::RestoreAConnection(CODBCConnect* pDBEngine)
  211. {
  212. EnterCriticalSection(&m_csBusyConnList); // 防止同时push和pop;
  213. if ( pDBEngine != NULL )
  214. {
  215. m_listBusyConnections.remove(pDBEngine);
  216. // 此处不需要EnterCriticalSection(m_csIdleConnList),否则会锁死;
  217. m_listIdleConnections.push_back(pDBEngine);
  218. }
  219. LeaveCriticalSection(&m_csBusyConnList);
  220. return 0;
  221. }
  222. void CODBCPool::StopWorkThread()
  223. {
  224. if ( m_hWorkThread )
  225. {
  226. m_bNeedStop = TRUE;
  227. // 因为线程是无限制等待信号的,所以这里先把标志位置为停止,然后发信号让线程检测;
  228. SetEvent(m_hHaveData);
  229. // 等待退出;
  230. WaitForSingleObject(m_hWorkThread, INFINITE);
  231. CloseHandle(m_hWorkThread);
  232. m_hWorkThread = NULL;
  233. CloseHandle(m_hHaveData);
  234. m_hHaveData = NULL;
  235. }
  236. }
  237. BOOL CODBCPool::IsNeedStop()
  238. {
  239. return m_bNeedStop;
  240. }
  241. BOOL CODBCPool::IsNeedConnection()
  242. {
  243. return m_bNeedConnection;
  244. }
  245. /************************************************************************/
  246. /* 维护线程; */
  247. /************************************************************************/
  248. DWORD WINAPI CODBCPool::WorkThread( LPVOID pdata)
  249. {
  250. CODBCPool * pConPool = (CODBCPool *) pdata;
  251. do
  252. {
  253. // 清理空闲连接对象;
  254. EnterCriticalSection(&pConPool->m_csIdleConnList);
  255. if ( pConPool->m_listBusyConnections.size() > 0 && pConPool->m_listBusyConnections.size() < pConPool->m_nMinCount )
  256. {
  257. if ( pConPool->m_listIdleConnections.size() == pConPool->m_nMinCount )
  258. {
  259. LeaveCriticalSection(&pConPool->m_csIdleConnList);
  260. continue;
  261. }
  262. // 从后面开始删除;
  263. CODBCConnect *pDBEngine = pConPool->m_listIdleConnections.back();
  264. pConPool->m_listIdleConnections.pop_back();
  265. //pDBEngine->Close();
  266. if ( pDBEngine)
  267. {
  268. //printf("删除连接对象成功%d\n",pConPool->m_nObjRef);
  269. delete pDBEngine;
  270. }
  271. InterlockedDecrement(&pConPool->m_nObjRef); // 减少计数;
  272. }
  273. LeaveCriticalSection(&pConPool->m_csIdleConnList);
  274. Sleep(500); // 降低CPU使用率;
  275. } while (WaitForSingleObject(pConPool->m_hHaveData, 500) == WAIT_TIMEOUT );
  276. return 0;
  277. }
  278. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  279. // 客户端调用的主接口;
  280. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  281. BOOL CODBCPool::GetSelectby(IN LPCTSTR lpTblName, IN LPCTSTR lpColumn, IN LPCTSTR lpFilters, OUT LPTSTR lpResult, IN CONST INT& nBufLen, IN CONST DWORD &dwTimeOut)
  282. {
  283. CODBCConnect *pDBConn = NULL;
  284. ODBCConnGuard oConnGuard(pDBConn, dwTimeOut);
  285. //此处为实际实现的接口,自己需要实现。
  286. if ( pDBConn == NULL)
  287. {
  288. return FALSE;
  289. }
  290. return pDBConn->GetSelectby(lpTblName, lpColumn, lpFilters, lpResult, nBufLen);
  291. }
  292. BOOL CODBCPool::ExecuteSQL( IN LPCTSTR lpExcuteSQL, IN CONST DWORD &dwTimeOut )
  293. {
  294. CODBCConnect *pDBConn = NULL;
  295. ODBCConnGuard oConnGuard(pDBConn, dwTimeOut);
  296. //此处为实际实现的接口,自己需要实现。
  297. if ( pDBConn == NULL)
  298. {
  299. return FALSE;
  300. }
  301. return pDBConn->ExecuteSQL(lpExcuteSQL);
  302. }
  303. INT CODBCPool::GetTblRecordCount( IN LPCTSTR lpTblName, IN LPCTSTR lpFilters, IN CONST DWORD &dwTimeOut )
  304. {
  305. CODBCConnect *pDBConn = NULL;
  306. ODBCConnGuard oConnGuard(pDBConn, dwTimeOut);
  307. if ( pDBConn == NULL)
  308. {
  309. return FALSE;
  310. }
  311. //此处为实际实现的接口,自己需要实现。
  312. return pDBConn->GetTblRecordCount(lpTblName, lpFilters);
  313. }
  314. BOOL CODBCPool::GetVersionInfo( OUT LPVOID lpVerInfo, IN CONST DWORD &dwTimeOut )
  315. {
  316. CODBCConnect *pDBConn = NULL;
  317. ODBCConnGuard oConnGuard(pDBConn, dwTimeOut);
  318. if ( pDBConn == NULL)
  319. {
  320. return FALSE;
  321. }
  322. //此处为实际实现的接口,自己需要实现。
  323. return pDBConn->GetVersionInfo(lpVerInfo);
  324. }
  325. BOOL CODBCPool::GetNetShareInfo( OUT LPVOID lpNetShareInfo, IN CONST DWORD &dwTimeOut )
  326. {
  327. CODBCConnect *pDBConn = NULL;
  328. ODBCConnGuard oConnGuard(pDBConn, dwTimeOut);
  329. if ( pDBConn == NULL)
  330. {
  331. return FALSE;
  332. }
  333. //此处为实际实现的接口,自己需要实现。
  334. return pDBConn->GetNetShareInfo(lpNetShareInfo);
  335. }
  336. BOOL CODBCPool::GetOrderInfo( OUT LPVOID lpOrderInfo, IN CONST DWORD &dwTimeOut )
  337. {
  338. //printf("\t\n【创建新守卫对象】\n");
  339. CODBCConnect *pDBConn = NULL;
  340. ODBCConnGuard oConnGuard(pDBConn, dwTimeOut);
  341. if ( pDBConn == NULL)
  342. {
  343. return FALSE;
  344. }
  345. //此处为实际实现的接口,自己需要实现。
  346. return pDBConn->GetOrderInfo(lpOrderInfo);
  347. }
  348. BOOL CODBCPool::GetunauthInfo( IN LPCTSTR lpSQLFilters, OUT LPTSTR lpUnauthInfo, IN CONST DWORD &dwTimeOut )
  349. {
  350. //printf("\t\n【创建新守卫对象】\n");
  351. CODBCConnect *pDBConn = NULL;
  352. ODBCConnGuard oConnGuard(pDBConn, dwTimeOut);
  353. if ( pDBConn == NULL)
  354. {
  355. return FALSE;
  356. }
  357. //此处为实际实现的接口,自己需要实现。
  358. return pDBConn->GetunauthInfo(lpSQLFilters, lpUnauthInfo);
  359. }
  360. BOOL CODBCPool::GetunauthInfo( IN LPCTSTR lpSQLFilters, OUT TString &strUnauthInfo, IN CONST DWORD &dwTimeOut )
  361. {
  362. //printf("\t\n【创建新守卫对象】\n");
  363. CODBCConnect *pDBConn = NULL;
  364. ODBCConnGuard oConnGuard(pDBConn, dwTimeOut);
  365. if ( pDBConn == NULL)
  366. {
  367. return FALSE;
  368. }
  369. //此处为实际实现的接口,自己需要实现。
  370. return pDBConn->GetunauthInfo(lpSQLFilters, strUnauthInfo);
  371. }
  372. INT CODBCPool::GetClientEnterpriseName( IN LPCTSTR lpSQLFilters, OUT LPVOID lpClientIPInfo, IN CONST DWORD &dwTimeOut )
  373. {
  374. CODBCConnect *pDBConn = NULL;
  375. ODBCConnGuard oConnGuard(pDBConn, dwTimeOut);
  376. if ( pDBConn == NULL)
  377. {
  378. return FALSE;
  379. }
  380. //此处为实际实现的接口,自己需要实现。
  381. return pDBConn->GetClientEnterpriseName(lpSQLFilters, lpClientIPInfo);
  382. }
  383. INT CODBCPool::GetEnterprisInfo(IN LPCTSTR lpEnterpriseName, OUT CArray<CStringArray, CStringArray>& AryEnterpriseInfo, IN CONST DWORD &dwTimeOut /* = 30000 */)
  384. {
  385. CODBCConnect *pDBConn = NULL;
  386. ODBCConnGuard oConnGuard(pDBConn, dwTimeOut);
  387. if ( pDBConn == NULL)
  388. {
  389. return FALSE;
  390. }
  391. //此处为实际实现的接口,自己需要实现。
  392. return pDBConn->GetEnterprisInfo(lpEnterpriseName, AryEnterpriseInfo);
  393. }
  394. BOOL CODBCPool::GetDBCFileInfo(IN LPCTSTR lpSQL, OUT TString& strResult, IN CONST DWORD &dwTimeOut)
  395. {
  396. CODBCConnect *pDBConn = NULL;
  397. ODBCConnGuard oConnGuard(pDBConn, dwTimeOut);
  398. if ( pDBConn == NULL)
  399. {
  400. return FALSE;
  401. }
  402. return pDBConn->GetDBCFileInfo(lpSQL, strResult);
  403. }
  404. BOOL CODBCPool::GetTookOrderInfo( OUT LPVOID lpTookOrderInfo, IN CONST DWORD &dwTimeOut /* = 3000 */ )
  405. {
  406. CODBCConnect *pDBConn = NULL;
  407. ODBCConnGuard oConnGuard(pDBConn, dwTimeOut);
  408. if ( pDBConn == NULL)
  409. {
  410. return FALSE;
  411. }
  412. //此处为实际实现的接口,自己需要实现。
  413. return pDBConn->GetTookOrderInfo(lpTookOrderInfo);
  414. }