CDBConnPool.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. #include "CDBConnPool.h"
  2. #include <boost/bind.hpp>
  3. #include <boost/lockfree/queue.hpp>
  4. namespace YADB
  5. {
  6. boost::lockfree::queue<_ASYNC_SQL_*, boost::lockfree::capacity<MAX_ASYNC_QUEQUE_CAPACITY>> __AsyncQueue;//异步执行无锁队列
  7. CDBConnPool::CDBConnPool()
  8. {
  9. __pAsyncDBConn = 0;
  10. }
  11. CDBConnPool::~CDBConnPool()
  12. {
  13. Close();
  14. }
  15. CDBConnect * CDBConnPool::__CreateIdleConn( std::string& Error, bool IsTemp )
  16. {
  17. std::string ConnErr;
  18. _DB_CONN_SETTING_ ConnSetting = static_cast< _DB_CONN_SETTING_ >(__Setting);
  19. CDBConnect* pConn = new CDBConnect( IsTemp );
  20. if ( !pConn->Connect( ConnSetting, ConnErr ) )
  21. {
  22. delete pConn;
  23. pConn = 0;
  24. return 0;
  25. }
  26. //如果设置了预处理SQL要准备预处理
  27. if ( !ConnSetting.stmtSQL.empty() )
  28. {
  29. if ( !pConn->Preparestmt( ConnSetting.stmtSQL.c_str(), Error ) )
  30. {
  31. delete pConn;
  32. pConn = 0;
  33. return 0;
  34. }
  35. }
  36. __IdleConnList.push_back( pConn );
  37. return pConn;
  38. }
  39. bool CDBConnPool::Create( const _DB_POOL_SETTING_& Setting, std::string& Error )
  40. {
  41. std::unique_lock<std::mutex> lock( __mtx );
  42. if ( Setting.PoolSize < DCC_MIN_COUNT )
  43. {
  44. Error = "PoolSize is too small!";
  45. return false;
  46. }
  47. if ( Setting.PoolSize > DCC_MAX_COUNT )
  48. {
  49. Error = "PoolSize is too big!";
  50. return false;
  51. }
  52. __Setting = Setting;
  53. std::string ConnErr;
  54. for ( int i = 0; i < __Setting.PoolSize; i++ )
  55. {
  56. CDBConnect* pConn = __CreateIdleConn( Error );
  57. if ( !pConn )
  58. {
  59. return false;
  60. }
  61. }
  62. //创建异步执行线程
  63. __CreateAsyncThrdConn();
  64. //启动异步执行线程
  65. __StartAsyncThrd();
  66. return true;
  67. }
  68. void CDBConnPool::Close()
  69. {
  70. std::unique_lock<std::mutex> lock( __mtx );
  71. //停止异步执行线程
  72. __StopAsyncThrd();
  73. //刪除异步执行线程连接
  74. __DestroyAsyncThrdConn();
  75. //把所有列表中的连接对象都关闭删除并清除列表
  76. CDBConnect* pConn = 0;
  77. std::list<CDBConnect*>::iterator lit_conn;
  78. for ( lit_conn = __BusyConnList.begin(); lit_conn != __BusyConnList.end(); lit_conn++ )
  79. {
  80. pConn = *lit_conn;
  81. pConn->Close();
  82. delete pConn;
  83. pConn = 0;
  84. }
  85. __BusyConnList.clear();
  86. for ( lit_conn = __IdleConnList.begin(); lit_conn != __IdleConnList.end(); lit_conn++ )
  87. {
  88. pConn = *lit_conn;
  89. pConn->Close();
  90. delete pConn;
  91. pConn = 0;
  92. }
  93. __IdleConnList.clear();
  94. }
  95. CDBConnect * CDBConnPool::GetDBConnect( std::string& Error )
  96. {
  97. std::unique_lock<std::mutex> lock( __mtx );
  98. CDBConnect* pConn = 0;
  99. if ( __IdleConnList.size() > 0 )
  100. {
  101. pConn = *(__IdleConnList.begin());
  102. __IdleConnList.pop_front();
  103. __BusyConnList.push_back( pConn );
  104. }
  105. else
  106. {
  107. //如果已经没有空闲连接,只要当前连接池数量没有超过最大连接数就创建一个临时连接
  108. if ( __IdleConnList.size() < DCC_MAX_COUNT )
  109. {
  110. pConn = __CreateIdleConn( Error, true );
  111. if ( !pConn )
  112. {
  113. Error = "Error,failed connect to database!";
  114. return 0;
  115. }
  116. __IdleConnList.pop_front();
  117. __BusyConnList.push_back( pConn );
  118. }
  119. else
  120. {
  121. Error = "Error,db connect count beyond the max connect count!";
  122. return 0;
  123. }
  124. }
  125. //验证看数据库连接是否还有效
  126. if ( pConn )
  127. {
  128. if ( pConn->ConnctionTest( Error ) != 0 )
  129. {
  130. //重连一次
  131. _DB_CONN_SETTING_ ConnSetting = static_cast< _DB_CONN_SETTING_ >(__Setting);
  132. pConn->Close();
  133. int nRet = pConn->Connect( ConnSetting, Error );
  134. if ( nRet < 0 )
  135. {
  136. GiveBack( pConn );
  137. Error = "Error,failed connect to database!";
  138. return 0;
  139. }
  140. }
  141. }
  142. return pConn;
  143. }
  144. void CDBConnPool::GiveBack( CDBConnect * pConn )
  145. {
  146. std::unique_lock<std::mutex> lock( __mtx );
  147. if ( 0 == pConn )
  148. {
  149. return;
  150. }
  151. __BusyConnList.remove( pConn );
  152. //如果是临时连接,直接删除不再放入到空闲连接列表中
  153. if ( pConn->IsTemp() )
  154. {
  155. delete pConn;
  156. pConn = 0;
  157. }
  158. else
  159. {
  160. __IdleConnList.push_back( pConn );
  161. }
  162. }
  163. void CDBConnPool::_AsyncThreadFunc( CDBConnPool* pOwner )
  164. {
  165. std::string Error;
  166. while( pOwner->__Running )
  167. {
  168. _ASYNC_SQL_* pData = 0;
  169. while ( __AsyncQueue.pop( pData ) )
  170. {
  171. if ( pData )
  172. {
  173. if ( __pAsyncDBConn )
  174. {
  175. my_ulonglong llRes = 0;
  176. llRes = __pAsyncDBConn->ExecuteRealSql( pData->SQL.c_str(), Error );
  177. if ( -1 == llRes )
  178. {
  179. //Execute failed, write log...
  180. printf( "Error,调用ExcuteRealSql失败,Err=%s\n", Error.c_str() );
  181. //如果失败了看是不是数据库断开连接了,尝试重新连接一次
  182. if ( __pAsyncDBConn->ConnctionTest( Error ) != 0 )
  183. {
  184. _DB_CONN_SETTING_ ConnSetting = static_cast< _DB_CONN_SETTING_ >(__Setting);
  185. __pAsyncDBConn->Close();
  186. int nRet = __pAsyncDBConn->Connect( ConnSetting, Error );
  187. if ( nRet < 0 )
  188. {
  189. Error = "Error,failed connect to database!";
  190. //Connect failed, write log...
  191. printf( "Error,failed connect to database,Err=%s\n", Error.c_str() );
  192. //如果连接失败了休息一下
  193. boost::this_thread::sleep( boost::posix_time::milliseconds( 100 ) );
  194. }
  195. }
  196. //如果执行失败,失败次数加一,失败次数小于最大失败次数放到队尾下次再执行
  197. pData->FailedCount++;
  198. if ( pData->FailedCount < MAX_ASYNC_EXEC_FAILED_COUNT )
  199. {
  200. _ASYNC_SQL_* pNewData = new _ASYNC_SQL_();
  201. pNewData->FailedCount = pData->FailedCount;
  202. pNewData->SQL = pData->SQL;
  203. __AsyncQueue.push( pNewData );
  204. }
  205. }
  206. }
  207. delete pData;
  208. pData = 0;
  209. }
  210. }
  211. boost::this_thread::sleep_for( boost::chrono::milliseconds( 1 ) );
  212. }
  213. //线程退出
  214. __IsExited = true;
  215. }
  216. void CDBConnPool::__StopAsyncThrd()
  217. {
  218. if ( !__Running )
  219. {
  220. return;
  221. }
  222. //等待异步执行线程退出
  223. __Running = false;
  224. while ( !__IsExited )
  225. {
  226. boost::this_thread::sleep_for( boost::chrono::milliseconds( 1 ) );
  227. }
  228. //把异步执行无锁队列中每个元素释放
  229. _ASYNC_SQL_* pData = 0;
  230. while ( __AsyncQueue.pop( pData ) )
  231. {
  232. if (pData)
  233. {
  234. delete pData;
  235. pData = 0;
  236. }
  237. }
  238. }
  239. void CDBConnPool::__StartAsyncThrd()
  240. {
  241. boost::thread thrd( boost::bind( &CDBConnPool::_AsyncThreadFunc, this, this ) );
  242. thrd.detach();
  243. }
  244. void CDBConnPool::__CreateAsyncThrdConn()
  245. {
  246. std::string ConnErr;
  247. _DB_CONN_SETTING_ ConnSetting = static_cast< _DB_CONN_SETTING_ >(__Setting);
  248. CDBConnect* pConn = new CDBConnect();
  249. if ( !pConn->Connect( ConnSetting, ConnErr ) )
  250. {
  251. delete pConn;
  252. pConn = 0;
  253. return;
  254. }
  255. __pAsyncDBConn = pConn;
  256. }
  257. void CDBConnPool::__DestroyAsyncThrdConn()
  258. {
  259. if ( __pAsyncDBConn )
  260. {
  261. __pAsyncDBConn->Close();
  262. delete __pAsyncDBConn;
  263. __pAsyncDBConn = 0;
  264. }
  265. }
  266. bool CDBConnPool::PushAsync( const std::string& strSQL )
  267. {
  268. _ASYNC_SQL_* pData = new _ASYNC_SQL_;
  269. if ( !pData )
  270. {
  271. return false;
  272. }
  273. pData->SQL = strSQL;
  274. return __AsyncQueue.push( pData );
  275. }
  276. bool CDBConnPool::Query( const char *szSql, CDBResultSet& DBRes, std::string& Error )
  277. {
  278. CDBConnect *pConn = GetDBConnect( Error );
  279. if ( 0 == pConn )
  280. {
  281. return false;
  282. }
  283. MYSQL_RES* pRes = pConn->Query( szSql, Error );
  284. GiveBack( pConn );
  285. return DBRes.Bind( pRes, Error );
  286. }
  287. MYSQL_RES* CDBConnPool::Query( const char *szSql, std::string& Error )
  288. {
  289. CDBConnect *pConn = GetDBConnect( Error );
  290. if ( 0 == pConn )
  291. {
  292. return false;
  293. }
  294. MYSQL_RES* pRes = pConn->Query( szSql, Error );
  295. GiveBack( pConn );
  296. return pRes;
  297. }
  298. my_ulonglong CDBConnPool::ExecuteSql( const char *szSql, std::string& Error )
  299. {
  300. CDBConnect *pConn = GetDBConnect( Error );
  301. if ( 0 == pConn )
  302. {
  303. return -1;
  304. }
  305. my_ulonglong nRet = pConn->ExecuteSql( szSql, Error );
  306. GiveBack( pConn );
  307. return nRet;
  308. }
  309. my_ulonglong CDBConnPool::ExecuteSqlID( const char * szSql, std::string & Error )
  310. {
  311. CDBConnect *pConn = GetDBConnect( Error );
  312. if ( 0 == pConn )
  313. {
  314. return -1;
  315. }
  316. my_ulonglong nRet = pConn->ExecuteSqlID( szSql, Error );
  317. GiveBack( pConn );
  318. return nRet;
  319. }
  320. }