CDBConnPool.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  1. <<<<<<< HEAD
  2. #include "CDBConnPool.h"
  3. #include <boost/bind.hpp>
  4. #include <boost/lockfree/queue.hpp>
  5. #include <stdio.h>
  6. namespace YADB
  7. {
  8. boost::lockfree::queue<_ASYNC_SQL_*, boost::lockfree::capacity<MAX_ASYNC_QUEQUE_CAPACITY>> __AsyncQueue;//寮傛�鎵ц�鏃犻攣闃熷垪
  9. CDBConnPool::CDBConnPool()
  10. {
  11. __pAsyncDBConn = 0;
  12. printf("connpool ....\n");
  13. }
  14. CDBConnPool::~CDBConnPool()
  15. {
  16. Close();
  17. }
  18. CDBConnect * CDBConnPool::__CreateIdleConn( std::string& Error, bool IsTemp )
  19. {
  20. std::string ConnErr;
  21. _DB_CONN_SETTING_ ConnSetting = static_cast< _DB_CONN_SETTING_ >(__Setting);
  22. CDBConnect* pConn = new CDBConnect( IsTemp );
  23. if ( !pConn->Connect( ConnSetting, ConnErr ) )
  24. {
  25. delete pConn;
  26. pConn = 0;
  27. return 0;
  28. }
  29. //濡傛灉璁剧疆浜嗛�澶勭悊SQL瑕佸噯澶囬�澶勭悊
  30. if ( !ConnSetting.stmtSQL.empty() )
  31. {
  32. if ( !pConn->Preparestmt( ConnSetting.stmtSQL.c_str(), Error ) )
  33. {
  34. delete pConn;
  35. pConn = 0;
  36. return 0;
  37. }
  38. }
  39. __IdleConnList.push_back( pConn );
  40. return pConn;
  41. }
  42. bool CDBConnPool::Create( const _DB_POOL_SETTING_& Setting, std::string& Error )
  43. {
  44. std::unique_lock<std::mutex> lock( __mtx );
  45. if ( Setting.PoolSize < DCC_MIN_COUNT )
  46. {
  47. Error = "PoolSize is too small!"+std::to_string(Setting.PoolSize);
  48. return false;
  49. }
  50. if ( Setting.PoolSize > DCC_MAX_COUNT )
  51. {
  52. Error = "PoolSize is too big!";
  53. return false;
  54. }
  55. __Setting = Setting;
  56. std::string ConnErr;
  57. for ( int i = 0; i < __Setting.PoolSize; i++ )
  58. {
  59. CDBConnect* pConn = __CreateIdleConn( Error );
  60. if ( !pConn )
  61. {
  62. return false;
  63. }
  64. }
  65. //鍒涘缓寮傛�鎵ц�绾跨▼
  66. __CreateAsyncThrdConn();
  67. //鍚�姩寮傛�鎵ц�绾跨▼
  68. __StartAsyncThrd();
  69. return true;
  70. }
  71. void CDBConnPool::Close()
  72. {
  73. std::unique_lock<std::mutex> lock( __mtx );
  74. //鍋滄�寮傛�鎵ц�绾跨▼
  75. __StopAsyncThrd();
  76. //鍒�櫎寮傛�鎵ц�绾跨▼杩炴帴
  77. __DestroyAsyncThrdConn();
  78. //鎶婃墍鏈夊垪琛ㄤ腑鐨勮繛鎺ュ�璞¢兘鍏抽棴鍒犻櫎骞舵竻闄ゅ垪琛�
  79. CDBConnect* pConn = 0;
  80. std::list<CDBConnect*>::iterator lit_conn;
  81. for ( lit_conn = __BusyConnList.begin(); lit_conn != __BusyConnList.end(); lit_conn++ )
  82. {
  83. pConn = *lit_conn;
  84. pConn->Close();
  85. delete pConn;
  86. pConn = 0;
  87. }
  88. __BusyConnList.clear();
  89. for ( lit_conn = __IdleConnList.begin(); lit_conn != __IdleConnList.end(); lit_conn++ )
  90. {
  91. pConn = *lit_conn;
  92. pConn->Close();
  93. delete pConn;
  94. pConn = 0;
  95. }
  96. __IdleConnList.clear();
  97. }
  98. CDBConnect * CDBConnPool::GetDBConnect( std::string& Error )
  99. {
  100. std::unique_lock<std::mutex> lock( __mtx );
  101. CDBConnect* pConn = 0;
  102. if ( __IdleConnList.size() > 0 )
  103. {
  104. pConn = *(__IdleConnList.begin());
  105. __IdleConnList.pop_front();
  106. __BusyConnList.push_back( pConn );
  107. }
  108. else
  109. {
  110. //濡傛灉宸茬粡娌℃湁绌洪棽杩炴帴,鍙��褰撳墠杩炴帴姹犳暟閲忔病鏈夎秴杩囨渶澶ц繛鎺ユ暟灏卞垱寤轰竴涓�复鏃惰繛鎺�
  111. if ( __IdleConnList.size() < DCC_MAX_COUNT )
  112. {
  113. pConn = __CreateIdleConn( Error, true );
  114. if ( !pConn )
  115. {
  116. Error = "Error,failed connect to database!";
  117. return 0;
  118. }
  119. __IdleConnList.pop_front();
  120. __BusyConnList.push_back( pConn );
  121. }
  122. else
  123. {
  124. Error = "Error,db connect count beyond the max connect count!";
  125. return 0;
  126. }
  127. }
  128. //楠岃瘉鐪嬫暟鎹�簱杩炴帴鏄�惁杩樻湁鏁�
  129. if ( pConn )
  130. {
  131. if ( pConn->ConnctionTest( Error ) != 0 )
  132. {
  133. //閲嶈繛涓€娆�
  134. _DB_CONN_SETTING_ ConnSetting = static_cast< _DB_CONN_SETTING_ >(__Setting);
  135. pConn->Close();
  136. int nRet = pConn->Connect( ConnSetting, Error );
  137. if ( nRet < 0 )
  138. {
  139. GiveBack( pConn );
  140. Error = "Error,failed connect to database!";
  141. return 0;
  142. }
  143. }
  144. }
  145. return pConn;
  146. }
  147. void CDBConnPool::GiveBack( CDBConnect * pConn )
  148. {
  149. std::unique_lock<std::mutex> lock( __mtx );
  150. if ( 0 == pConn )
  151. {
  152. return;
  153. }
  154. __BusyConnList.remove( pConn );
  155. //濡傛灉鏄�复鏃惰繛鎺�,鐩存帴鍒犻櫎涓嶅啀鏀惧叆鍒扮┖闂茶繛鎺ュ垪琛ㄤ腑
  156. if ( pConn->IsTemp() )
  157. {
  158. delete pConn;
  159. pConn = 0;
  160. }
  161. else
  162. {
  163. __IdleConnList.push_back( pConn );
  164. }
  165. }
  166. void CDBConnPool::_AsyncThreadFunc( CDBConnPool* pOwner )
  167. {
  168. std::string Error;
  169. while( pOwner->__Running )
  170. {
  171. _ASYNC_SQL_* pData = 0;
  172. while ( __AsyncQueue.pop( pData ) )
  173. {
  174. if ( pData )
  175. {
  176. if ( __pAsyncDBConn )
  177. {
  178. my_ulonglong llRes = 0;
  179. llRes = __pAsyncDBConn->ExecuteRealSql( pData->SQL.c_str(), Error );
  180. if ( -1 == llRes )
  181. {
  182. //Execute failed, write log...
  183. printf( "Error,璋冪敤ExcuteRealSql澶辫触,Err=%s\n", Error.c_str() );
  184. //濡傛灉澶辫触浜嗙湅鏄�笉鏄�暟鎹�簱鏂�紑杩炴帴浜嗭紝灏濊瘯閲嶆柊杩炴帴涓€娆�
  185. if ( __pAsyncDBConn->ConnctionTest( Error ) != 0 )
  186. {
  187. _DB_CONN_SETTING_ ConnSetting = static_cast< _DB_CONN_SETTING_ >(__Setting);
  188. __pAsyncDBConn->Close();
  189. int nRet = __pAsyncDBConn->Connect( ConnSetting, Error );
  190. if ( nRet < 0 )
  191. {
  192. Error = "Error,failed connect to database!";
  193. //Connect failed, write log...
  194. printf( "Error,failed connect to database,Err=%s\n", Error.c_str() );
  195. //濡傛灉杩炴帴澶辫触浜嗕紤鎭�竴涓�
  196. boost::this_thread::sleep( boost::posix_time::milliseconds( 100 ) );
  197. }
  198. }
  199. //濡傛灉鎵ц�澶辫触锛屽け璐ユ�鏁板姞涓€锛屽け璐ユ�鏁板皬浜庢渶澶уけ璐ユ�鏁版斁鍒伴槦灏句笅娆″啀鎵ц�
  200. pData->FailedCount++;
  201. if ( pData->FailedCount < MAX_ASYNC_EXEC_FAILED_COUNT )
  202. {
  203. _ASYNC_SQL_* pNewData = new _ASYNC_SQL_();
  204. pNewData->FailedCount = pData->FailedCount;
  205. pNewData->SQL = pData->SQL;
  206. __AsyncQueue.push( pNewData );
  207. }
  208. }
  209. }
  210. delete pData;
  211. pData = 0;
  212. }
  213. }
  214. boost::this_thread::sleep_for( boost::chrono::milliseconds( 1 ) );
  215. }
  216. //绾跨▼閫€鍑�
  217. __IsExited = true;
  218. }
  219. void CDBConnPool::__StopAsyncThrd()
  220. {
  221. if ( !__Running )
  222. {
  223. return;
  224. }
  225. //绛夊緟寮傛�鎵ц�绾跨▼閫€鍑�
  226. __Running = false;
  227. while ( !__IsExited )
  228. {
  229. boost::this_thread::sleep_for( boost::chrono::milliseconds( 1 ) );
  230. }
  231. //鎶婂紓姝ユ墽琛屾棤閿侀槦鍒椾腑姣忎釜鍏冪礌閲婃斁
  232. _ASYNC_SQL_* pData = 0;
  233. while ( __AsyncQueue.pop( pData ) )
  234. {
  235. if (pData)
  236. {
  237. delete pData;
  238. pData = 0;
  239. }
  240. }
  241. }
  242. void CDBConnPool::__StartAsyncThrd()
  243. {
  244. boost::thread thrd( boost::bind( &CDBConnPool::_AsyncThreadFunc, this, this ) );
  245. thrd.detach();
  246. }
  247. void CDBConnPool::__CreateAsyncThrdConn()
  248. {
  249. std::string ConnErr;
  250. _DB_CONN_SETTING_ ConnSetting = static_cast< _DB_CONN_SETTING_ >(__Setting);
  251. CDBConnect* pConn = new CDBConnect();
  252. if ( !pConn->Connect( ConnSetting, ConnErr ) )
  253. {
  254. delete pConn;
  255. pConn = 0;
  256. return;
  257. }
  258. __pAsyncDBConn = pConn;
  259. }
  260. void CDBConnPool::__DestroyAsyncThrdConn()
  261. {
  262. if ( __pAsyncDBConn )
  263. {
  264. __pAsyncDBConn->Close();
  265. delete __pAsyncDBConn;
  266. __pAsyncDBConn = 0;
  267. }
  268. }
  269. bool CDBConnPool::PushAsync( const std::string& strSQL )
  270. {
  271. _ASYNC_SQL_* pData = new _ASYNC_SQL_;
  272. if ( !pData )
  273. {
  274. return false;
  275. }
  276. pData->SQL = strSQL;
  277. return __AsyncQueue.push( pData );
  278. }
  279. bool CDBConnPool::Query( const char *szSql, CDBResultSet& DBRes, std::string& Error )
  280. {
  281. CDBConnect *pConn = GetDBConnect( Error );
  282. if ( 0 == pConn )
  283. {
  284. return false;
  285. }
  286. MYSQL_RES* pRes = pConn->Query( szSql, Error );
  287. GiveBack( pConn );
  288. return DBRes.Bind( pRes, Error );
  289. }
  290. MYSQL_RES* CDBConnPool::Query( const char *szSql, std::string& Error )
  291. {
  292. CDBConnect *pConn = GetDBConnect( Error );
  293. if ( 0 == pConn )
  294. {
  295. return false;
  296. }
  297. MYSQL_RES* pRes = pConn->Query( szSql, Error );
  298. GiveBack( pConn );
  299. return pRes;
  300. }
  301. my_ulonglong CDBConnPool::ExecuteSql( const char *szSql, std::string& Error )
  302. {
  303. CDBConnect *pConn = GetDBConnect( Error );
  304. if ( 0 == pConn )
  305. {
  306. return -1;
  307. }
  308. my_ulonglong nRet = pConn->ExecuteSql( szSql, Error );
  309. GiveBack( pConn );
  310. return nRet;
  311. }
  312. my_ulonglong CDBConnPool::ExecuteSqlID( const char * szSql, std::string & Error )
  313. {
  314. CDBConnect *pConn = GetDBConnect( Error );
  315. if ( 0 == pConn )
  316. {
  317. return -1;
  318. }
  319. my_ulonglong nRet = pConn->ExecuteSqlID( szSql, Error );
  320. GiveBack( pConn );
  321. return nRet;
  322. }
  323. }
  324. =======
  325. #include "CDBConnPool.h"
  326. #include <boost/bind.hpp>
  327. namespace YADB
  328. {
  329. boost::lockfree::queue<_ASYNC_SQL_*, boost::lockfree::capacity<MAX_ASYNC_QUEQUE_CAPACITY>> __AsyncQueue;//异步执行无锁队列
  330. CDBConnPool::CDBConnPool()
  331. {
  332. __pAsyncDBConn = 0;
  333. }
  334. CDBConnPool::~CDBConnPool()
  335. {
  336. Close();
  337. }
  338. CDBConnect * CDBConnPool::__CreateIdleConn( std::string& ConnErr, bool IsTemp )
  339. {
  340. //std::string ConnErr;
  341. _DB_CONN_SETTING_ ConnSetting = static_cast< _DB_CONN_SETTING_ >(__Setting);
  342. CDBConnect* pConn = new CDBConnect( IsTemp );
  343. if ( !pConn->Connect( ConnSetting, ConnErr ) )
  344. {
  345. delete pConn;
  346. pConn = 0;
  347. return 0;
  348. }
  349. //如果设置了预处理SQL要准备预处理
  350. if ( !ConnSetting.stmtSQL.empty() )
  351. {
  352. if ( !pConn->Preparestmt( ConnSetting.stmtSQL.c_str(), ConnErr ) )
  353. {
  354. delete pConn;
  355. pConn = 0;
  356. return 0;
  357. }
  358. }
  359. __IdleConnList.push_back( pConn );
  360. return pConn;
  361. }
  362. bool CDBConnPool::Create( const _DB_POOL_SETTING_& Setting, std::string& szError )
  363. {
  364. return Create(Setting,true,szError);
  365. }
  366. bool CDBConnPool::Create( const _DB_POOL_SETTING_& Setting,bool bAsync, std::string& szError )
  367. {
  368. std::unique_lock<std::mutex> lock( __mtx );
  369. if ( Setting.PoolSize < DCC_MIN_COUNT )
  370. {
  371. szError = "PoolSize is too small!";
  372. return false;
  373. }
  374. if ( Setting.PoolSize > DCC_MAX_COUNT )
  375. {
  376. szError = "PoolSize is too big!";
  377. return false;
  378. }
  379. __Setting = Setting;
  380. //检查连接池中是否已有连接数量
  381. if ((int)__IdleConnList.size() < __Setting.PoolSize)
  382. {
  383. for ( int i = 0; i < __Setting.PoolSize; i++ )
  384. {
  385. CDBConnect* pConn = __CreateIdleConn( szError );
  386. if ( !pConn )
  387. {
  388. return false;
  389. }
  390. }
  391. }
  392. //是否已创建异步线程
  393. if (bAsync || !__Running)
  394. {
  395. //创建异步执行线程
  396. __CreateAsyncThrdConn();
  397. //启动异步执行线程
  398. __StartAsyncThrd();
  399. }
  400. return true;
  401. }
  402. void CDBConnPool::Close()
  403. {
  404. std::unique_lock<std::mutex> lock( __mtx );
  405. //停止异步执行线程
  406. __StopAsyncThrd();
  407. //??????????步执行线程连接
  408. __DestroyAsyncThrdConn();
  409. //把所有列表中的连接对象都关闭删除并清除列表
  410. CDBConnect* pConn = 0;
  411. std::list<CDBConnect*>::iterator lit_conn;
  412. for ( lit_conn = __BusyConnList.begin(); lit_conn != __BusyConnList.end(); lit_conn++ )
  413. {
  414. pConn = *lit_conn;
  415. pConn->Close();
  416. delete pConn;
  417. pConn = 0;
  418. }
  419. __BusyConnList.clear();
  420. for ( lit_conn = __IdleConnList.begin(); lit_conn != __IdleConnList.end(); lit_conn++ )
  421. {
  422. pConn = *lit_conn;
  423. pConn->Close();
  424. delete pConn;
  425. pConn = 0;
  426. }
  427. __IdleConnList.clear();
  428. }
  429. CDBConnect * CDBConnPool::GetDBConnect( std::string& Error )
  430. {
  431. std::unique_lock<std::mutex> lock( __mtx );
  432. CDBConnect* pConn = 0;
  433. if ( __IdleConnList.size() > 0 )
  434. {
  435. pConn = *(__IdleConnList.begin());
  436. __IdleConnList.pop_front();
  437. __BusyConnList.push_back( pConn );
  438. }
  439. else
  440. {
  441. //如果已经没有空闲连接,只要当前连接池数量没有超过最大连接数就创建一个临时连接
  442. //这个判断无意义
  443. if ( __IdleConnList.size() < DCC_MAX_COUNT )
  444. {
  445. pConn = __CreateIdleConn( Error, true );
  446. if ( !pConn )
  447. {
  448. Error = "Error,failed connect to database!";
  449. return 0;
  450. }
  451. __IdleConnList.pop_front();
  452. __BusyConnList.push_back( pConn );
  453. }
  454. else
  455. {
  456. Error = "Error,db connect count beyond the max connect count!";
  457. return 0;
  458. }
  459. }
  460. //验证看数据库连接是否还有效
  461. if ( pConn )
  462. {
  463. if ( pConn->ConnctionTest( Error ) != 0 )
  464. {
  465. //重连一次
  466. _DB_CONN_SETTING_ ConnSetting = static_cast< _DB_CONN_SETTING_ >(__Setting);
  467. pConn->Close();
  468. int nRet = pConn->Connect( ConnSetting, Error );
  469. if ( nRet < 0 )
  470. {
  471. GiveBack( pConn );
  472. Error = "Error,failed connect to database!";
  473. return 0;
  474. }
  475. }
  476. }
  477. return pConn;
  478. }
  479. void CDBConnPool::GiveBack( CDBConnect * pConn )
  480. {
  481. std::unique_lock<std::mutex> lock( __mtx );
  482. if ( 0 == pConn )
  483. {
  484. return;
  485. }
  486. __BusyConnList.remove( pConn );
  487. //如果是临时连接,直接删除不再放入到空闲连接列表中
  488. if ( pConn->IsTemp() )
  489. {
  490. delete pConn;
  491. pConn = 0;
  492. }
  493. else
  494. {
  495. __IdleConnList.push_back( pConn );
  496. }
  497. }
  498. void CDBConnPool::_AsyncThreadFunc( CDBConnPool* pOwner )
  499. {
  500. std::string Error;
  501. while( pOwner->__Running )
  502. {
  503. _ASYNC_SQL_* pData = 0;
  504. while ( __AsyncQueue.pop( pData ) )
  505. {
  506. if ( pData )
  507. {
  508. if ( __pAsyncDBConn )
  509. {
  510. my_ulonglong llRes = 0;
  511. llRes = __pAsyncDBConn->ExecuteRealSql( pData->SQL.c_str(), Error );
  512. if ( (my_ulonglong)-1 == llRes )
  513. {
  514. //Execute failed, write log...
  515. printf( "Error,调用ExcuteRealSql失败,Err=%s\n", Error.c_str() );
  516. //如果失败了看是不是数据库断开连接了,尝试重新连接一次
  517. if ( __pAsyncDBConn->ConnctionTest( Error ) != 0 )
  518. {
  519. _DB_CONN_SETTING_ ConnSetting = static_cast< _DB_CONN_SETTING_ >(__Setting);
  520. __pAsyncDBConn->Close();
  521. int nRet = __pAsyncDBConn->Connect( ConnSetting, Error );
  522. if ( nRet < 0 )
  523. {
  524. Error = "Error,failed connect to database!";
  525. //Connect failed, write log...
  526. printf( "Error,failed connect to database,Err=%s\n", Error.c_str() );
  527. //如果连接失败了休息一下
  528. boost::this_thread::sleep( boost::posix_time::milliseconds( 100 ) );
  529. }
  530. }
  531. //如果执行失败,失败次数加一,失败次数小于最大失败次数放到队尾下次再执行
  532. pData->FailedCount++;
  533. if ( pData->FailedCount < MAX_ASYNC_EXEC_FAILED_COUNT )
  534. {
  535. _ASYNC_SQL_* pNewData = new _ASYNC_SQL_();
  536. pNewData->FailedCount = pData->FailedCount;
  537. pNewData->SQL = pData->SQL;
  538. __AsyncQueue.push( pNewData );
  539. }
  540. }
  541. }
  542. delete pData;
  543. pData = 0;
  544. }
  545. }
  546. boost::this_thread::sleep_for( boost::chrono::milliseconds( 1 ) );
  547. }
  548. //线程退出
  549. __IsExited = true;
  550. }
  551. void CDBConnPool::__StopAsyncThrd()
  552. {
  553. if ( !__Running )
  554. {
  555. return;
  556. }
  557. //等待异步执行线程退出
  558. __Running = false;
  559. while ( !__IsExited )
  560. {
  561. boost::this_thread::sleep(boost::posix_time::millisec(1));
  562. }
  563. //把异步执行无锁队列中每个元素释放
  564. _ASYNC_SQL_* pData = 0;
  565. while ( __AsyncQueue.pop( pData ) )
  566. {
  567. if (pData)
  568. {
  569. delete pData;
  570. pData = 0;
  571. }
  572. }
  573. }
  574. void CDBConnPool::__StartAsyncThrd()
  575. {
  576. __Running = true;
  577. boost::thread thrd( boost::bind( &CDBConnPool::_AsyncThreadFunc, this, this ) );
  578. thrd.detach();
  579. }
  580. void CDBConnPool::__CreateAsyncThrdConn()
  581. {
  582. std::string ConnErr;
  583. //先断开之前的连接
  584. this->__DestroyAsyncThrdConn();
  585. _DB_CONN_SETTING_ ConnSetting = static_cast< _DB_CONN_SETTING_ >(__Setting);
  586. CDBConnect* pConn = new CDBConnect();
  587. if ( !pConn->Connect( ConnSetting, ConnErr ) )
  588. {
  589. delete pConn;
  590. pConn = 0;
  591. return;
  592. }
  593. __pAsyncDBConn = pConn;
  594. }
  595. void CDBConnPool::__DestroyAsyncThrdConn()
  596. {
  597. if ( __pAsyncDBConn )
  598. {
  599. __pAsyncDBConn->Close();
  600. delete __pAsyncDBConn;
  601. __pAsyncDBConn = 0;
  602. }
  603. }
  604. bool CDBConnPool::PushAsync( const std::string& strSQL )
  605. {
  606. _ASYNC_SQL_* pData = new _ASYNC_SQL_;
  607. if ( !pData )
  608. {
  609. return false;
  610. }
  611. pData->SQL = strSQL;
  612. return __AsyncQueue.push( pData );
  613. }
  614. bool CDBConnPool::Query( const char *szSql, CDBResultSet& DBRes,std::string& Error )
  615. {
  616. CDBConnect *pConn = GetDBConnect( Error );
  617. if ( 0 == pConn )
  618. {
  619. return false;
  620. }
  621. MYSQL_RES* pRes = pConn->Query( szSql, Error );
  622. GiveBack( pConn );
  623. return DBRes.Bind( pRes, Error );
  624. }
  625. MYSQL_RES* CDBConnPool::Query( const char *szSql, std::string& Error)
  626. {
  627. CDBConnect *pConn = GetDBConnect(Error);
  628. if( 0 == pConn){
  629. return nullptr;
  630. }
  631. MYSQL_RES* pRes = pConn->Query(szSql,Error);
  632. GiveBack(pConn);
  633. return pRes;
  634. }
  635. my_ulonglong CDBConnPool::ExecuteSql( const char *szSql, std::string& Error )
  636. {
  637. CDBConnect *pConn = GetDBConnect( Error );
  638. if ( 0 == pConn )
  639. {
  640. return -1;
  641. }
  642. my_ulonglong nRet = pConn->ExecuteSql( szSql, Error );
  643. GiveBack( pConn );
  644. return nRet;
  645. //return pConn->ExecuteSql( szSql, Error );
  646. }
  647. my_ulonglong CDBConnPool::ExecuteSqlID( const char * szSql, std::string & Error )
  648. {
  649. CDBConnect *pConn = GetDBConnect( Error );
  650. if ( 0 == pConn )
  651. {
  652. return -1;
  653. }
  654. my_ulonglong nRet = pConn->ExecuteSqlID( szSql, Error );
  655. GiveBack( pConn );
  656. return nRet;
  657. }
  658. }
  659. >>>>>>> 10da641ebe4222984b0724854edeb8088ac7c5a7