database_controller.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. /*
  2. * @Description: 数据库操作类
  3. * @Author: yct
  4. * @Date: 2020-07-15 14:08:46
  5. * @LastEditTime: 2020-07-24 14:19:31
  6. * @LastEditors: yct
  7. */
  8. #include "database_controller.h"
  9. Database_controller::Database_controller()
  10. {
  11. m_database_controller_status = E_UNKNOWN;
  12. mp_reconnect_thread = nullptr;
  13. m_conn_pool_size = 0;
  14. }
  15. Database_controller::~Database_controller()
  16. {
  17. database_controller_uninit();
  18. }
  19. Error_manager Database_controller::database_controller_init()
  20. {
  21. return database_controller_init_from_protobuf(DATABASE_COMMUNICATION_PARAMETER_PATH);
  22. }
  23. Error_manager Database_controller::database_controller_init_from_protobuf(std::string prototxt_path)
  24. {
  25. parkspace_proto::Database_communication_configuration_all t_database_config;
  26. if(! proto_tool::read_proto_param(prototxt_path,t_database_config) )
  27. {
  28. return Error_manager(DB_PROTOBUF_ERROR,MINOR_ERROR,
  29. "database_controller_init_from_protobuf(std::string prototxt_path) failed");
  30. }
  31. else
  32. {
  33. Error_manager t_error;
  34. t_error=database_controller_init(t_database_config.database_communication_configurations().db_ip(),\
  35. t_database_config.database_communication_configurations().db_port(),\
  36. t_database_config.database_communication_configurations().db_username(),\
  37. t_database_config.database_communication_configurations().db_passwd(),\
  38. t_database_config.database_communication_configurations().db_name(),\
  39. t_database_config.database_communication_configurations().db_conn_pool_size());
  40. if ( t_error != Error_code::SUCCESS )
  41. {
  42. return t_error;
  43. }
  44. }
  45. return SUCCESS;
  46. }
  47. //初始化
  48. Error_manager Database_controller::database_controller_init(std::string ip, int port, std::string username, std::string pass, std::string db_name, int conn_size)
  49. {
  50. Error_manager t_error;
  51. //系统结束标记与连接池大小
  52. // mb_exit = false;
  53. m_conn_pool_size = conn_size;
  54. m_db_param.connect_string = std::string("tcp://").append(ip).append(":").append(std::to_string(port));
  55. m_db_param.username = username;
  56. m_db_param.pass = pass;
  57. m_db_param.db_name = db_name;
  58. if(sql::mysql::get_driver_instance() == nullptr)
  59. {
  60. //胡力 直接修改状态
  61. m_database_controller_status = E_FAULT;
  62. // mb_initialized = false;
  63. return DB_INIT_FAILED;
  64. return Error_manager(Error_code::DB_INIT_FAILED, Error_level::MAJOR_ERROR,
  65. "数据库初始化失败 Database_controller::database_controller_init error ");
  66. }
  67. ////创建补充 数据库连接的通路
  68. t_error = fill_up_connect_channel();
  69. //守护线程,检查连接状态并保持连接数量
  70. m_database_reconnect_condition.reset(false,false,false);
  71. mp_reconnect_thread = new std::thread(&Database_controller::database_reconnect_thread, this);
  72. return t_error;
  73. }
  74. //反初始化
  75. Error_manager Database_controller::database_controller_uninit()
  76. {
  77. // mb_exit = true;
  78. if(mp_reconnect_thread!=nullptr)
  79. {
  80. m_database_reconnect_condition.kill_all();
  81. }
  82. if(mp_reconnect_thread!=nullptr)
  83. {
  84. if(mp_reconnect_thread->joinable())
  85. mp_reconnect_thread->join();
  86. delete mp_reconnect_thread;
  87. mp_reconnect_thread = nullptr;
  88. }
  89. m_database_connect_channel_list.clear();
  90. return SUCCESS;
  91. }
  92. Database_controller::Database_controller_status Database_controller::get_database_controller_status()
  93. {
  94. return m_database_controller_status;
  95. }
  96. Error_manager Database_controller::check_status()
  97. {
  98. if(get_database_controller_status()==E_READY)
  99. {
  100. return SUCCESS;
  101. }
  102. else if(get_database_controller_status() == E_UNKNOWN)
  103. {
  104. return Error_manager(Error_code::DB_INIT_FAILED, Error_level::MINOR_ERROR,
  105. "数据库初始化失败 check_status() error ");
  106. }
  107. else if(get_database_controller_status() == E_DISCONNECT)
  108. {
  109. return Error_manager(Error_code::DB_CONNECT_FAILED, Error_level::MINOR_ERROR,
  110. "数据库连接失败 check_status() error ");
  111. }
  112. else if(get_database_controller_status() == E_FAULT)
  113. {
  114. return Error_manager(Error_code::DB_STATUS_ERROR, Error_level::MINOR_ERROR,
  115. "数据库状态错误 check_status() error ");
  116. }
  117. }
  118. //数据库连接状态
  119. bool Database_controller::is_connected()
  120. {
  121. if ( m_database_controller_status == E_READY)
  122. {
  123. return true;
  124. }
  125. else
  126. {
  127. return false;
  128. }
  129. //
  130. // updata_connect_channel();
  131. // return mb_connected;
  132. }
  133. //****** 增删改查功能 *******
  134. //增
  135. Error_manager Database_controller::sql_insert(std::string sql_str)
  136. {
  137. #ifdef COUT_SQL_STRING
  138. LOG(INFO) << " sql_str = "<< sql_str << " --- " << this;
  139. #endif
  140. Error_manager t_error;
  141. //huli
  142. t_error = check_status();
  143. if ( t_error != Error_code::SUCCESS )
  144. {
  145. return t_error;
  146. }
  147. boost::shared_ptr<Database_connect_channel> tp_database_connect_channel;
  148. t_error = occupancy_connect_channel(tp_database_connect_channel);
  149. if ( t_error != Error_code::SUCCESS )
  150. {
  151. return t_error;
  152. }
  153. char buf[1024];
  154. memset(buf, 0, 1024);
  155. try
  156. {
  157. tp_database_connect_channel->mp_connect_channel->setSchema(m_db_param.db_name);
  158. boost::scoped_ptr<sql::Statement> stmt(tp_database_connect_channel->mp_connect_channel->createStatement());
  159. stmt->execute(sql_str);
  160. giveback_connect_channel(tp_database_connect_channel);
  161. return SUCCESS;
  162. }
  163. catch (sql::SQLException &e)
  164. {
  165. giveback_connect_channel(tp_database_connect_channel);
  166. /* Use what() (derived from std::runtime_error) to fetch the error message */
  167. sprintf(buf, "# ERR: %s\n (MySQL error code: %d, SQLState: %s\nsql: %s", e.what(), e.getErrorCode(), e.getSQLState().c_str(), sql_str.c_str());
  168. usleep(1000* 3000);
  169. return Error_manager(DB_INSERT_FAILED, NEGLIGIBLE_ERROR, buf);
  170. }
  171. catch (std::runtime_error &e)
  172. {
  173. giveback_connect_channel(tp_database_connect_channel);
  174. sprintf(buf, "# ERR: %s\n ERR: runtime_error in %s \nsql: %s",e.what(),__FILE__, sql_str.c_str());
  175. return Error_manager(DB_INSERT_FAILED, NEGLIGIBLE_ERROR, buf);
  176. }
  177. // catch (std::exception &e)
  178. // {
  179. // sprintf(buf, "# ERR: %s\n ERR: Standard exception in %s ",e.what(),__FILE__);
  180. // return Error_manager(DB_INSERT_FAILED, NEGLIGIBLE_ERROR, buf);
  181. // }
  182. return SUCCESS;
  183. }
  184. //删
  185. Error_manager Database_controller::sql_delete(std::string sql_str)
  186. {
  187. #ifdef COUT_SQL_STRING
  188. LOG(INFO) << " sql_str = "<< sql_str << " --- " << this;
  189. #endif
  190. // 1.检查状态,一旦无可用连接,则主动创建一个,若依然失败则直接返回错误
  191. Error_manager t_error;
  192. //huli
  193. t_error = check_status();
  194. if ( t_error != Error_code::SUCCESS )
  195. {
  196. return t_error;
  197. }
  198. boost::shared_ptr<Database_connect_channel> tp_database_connect_channel;
  199. t_error = occupancy_connect_channel(tp_database_connect_channel);
  200. if ( t_error != Error_code::SUCCESS )
  201. {
  202. return t_error;
  203. }
  204. char buf[1024];
  205. memset(buf, 0, 1024);
  206. try
  207. {
  208. tp_database_connect_channel->mp_connect_channel->setSchema(m_db_param.db_name);
  209. boost::scoped_ptr<sql::Statement> stmt(tp_database_connect_channel->mp_connect_channel->createStatement());
  210. stmt->execute(sql_str);
  211. giveback_connect_channel(tp_database_connect_channel);
  212. return SUCCESS;
  213. }
  214. catch (sql::SQLException &e)
  215. {
  216. /* Use what() (derived from std::runtime_error) to fetch the error message */
  217. giveback_connect_channel(tp_database_connect_channel);
  218. sprintf(buf, "# ERR: %s\n (MySQL error code: %d, SQLState: %s\nsql: %s", e.what(), e.getErrorCode(), e.getSQLState().c_str(), sql_str.c_str());
  219. return Error_manager(DB_DELETE_FAILED, NEGLIGIBLE_ERROR, buf);
  220. }
  221. catch (std::runtime_error &e)
  222. {
  223. giveback_connect_channel(tp_database_connect_channel);
  224. sprintf(buf, "# ERR: %s\n ERR: runtime_error in %s \nsql: %s",e.what(),__FILE__, sql_str.c_str());
  225. return Error_manager(DB_DELETE_FAILED, NEGLIGIBLE_ERROR, buf);
  226. }
  227. return SUCCESS;
  228. }
  229. //改
  230. Error_manager Database_controller::sql_update(std::string sql_str)
  231. {
  232. #ifdef COUT_SQL_STRING
  233. LOG(INFO) << " sql_str = "<< sql_str << " --- " << this;
  234. #endif
  235. Error_manager t_error;
  236. //huli
  237. t_error = check_status();
  238. if ( t_error != Error_code::SUCCESS )
  239. {
  240. return t_error;
  241. }
  242. boost::shared_ptr<Database_connect_channel> tp_database_connect_channel;
  243. t_error = occupancy_connect_channel(tp_database_connect_channel);
  244. if ( t_error != Error_code::SUCCESS )
  245. {
  246. return t_error;
  247. }
  248. char buf[1024];
  249. memset(buf, 0, 1024);
  250. try
  251. {
  252. tp_database_connect_channel->mp_connect_channel->setSchema(m_db_param.db_name);
  253. boost::scoped_ptr<sql::Statement> stmt(tp_database_connect_channel->mp_connect_channel->createStatement());
  254. int affected_rows = stmt->executeUpdate(sql_str);
  255. giveback_connect_channel(tp_database_connect_channel);
  256. if (affected_rows > 0)
  257. {
  258. return SUCCESS;
  259. }
  260. else
  261. {
  262. return Error_manager(Error_code::DB_UPDATE_FAILED, Error_level::MINOR_ERROR,
  263. (std::string("数据库修改失败 Database_controller::sql_update error ")+sql_str).c_str());
  264. }
  265. }
  266. catch (sql::SQLException &e)
  267. {
  268. /* Use what() (derived from std::runtime_error) to fetch the error message */
  269. giveback_connect_channel(tp_database_connect_channel);
  270. sprintf(buf, "# ERR: %s\n (MySQL error code: %d, SQLState: %s\nsql: %s", e.what(), e.getErrorCode(), e.getSQLState().c_str(), sql_str.c_str());
  271. return Error_manager(DB_UPDATE_FAILED, NEGLIGIBLE_ERROR, buf);
  272. }
  273. catch (std::runtime_error &e)
  274. {
  275. giveback_connect_channel(tp_database_connect_channel);
  276. sprintf(buf, "# ERR: %s\n ERR: runtime_error in %s \nsql: %s",e.what(),__FILE__, sql_str.c_str());
  277. return Error_manager(DB_UPDATE_FAILED, NEGLIGIBLE_ERROR, buf);
  278. }
  279. return SUCCESS;
  280. }
  281. //查
  282. Error_manager Database_controller::sql_query(std::string sql_str, boost::shared_ptr< sql::ResultSet > &query_result)
  283. {
  284. #ifdef COUT_SQL_STRING
  285. LOG(INFO) << " sql_str = "<< sql_str << " --- " << this;
  286. #endif
  287. Error_manager t_error;
  288. //huli
  289. t_error = check_status();
  290. if ( t_error != Error_code::SUCCESS )
  291. {
  292. return t_error;
  293. }
  294. boost::shared_ptr<Database_connect_channel> tp_database_connect_channel;
  295. t_error = occupancy_connect_channel(tp_database_connect_channel);
  296. if ( t_error != Error_code::SUCCESS )
  297. {
  298. return t_error;
  299. }
  300. char buf[1024];
  301. memset(buf, 0, 1024);
  302. try
  303. {
  304. tp_database_connect_channel->mp_connect_channel->setSchema(m_db_param.db_name);
  305. boost::scoped_ptr<sql::Statement> stmt(tp_database_connect_channel->mp_connect_channel->createStatement());
  306. query_result = boost::shared_ptr< sql::ResultSet >(stmt->executeQuery(sql_str));
  307. giveback_connect_channel(tp_database_connect_channel);
  308. // 检查结果
  309. if(query_result==nullptr)
  310. {
  311. return Error_manager(Error_code::DB_QUERY_FAILED, Error_level::MAJOR_ERROR,
  312. (std::string("数据库查询失败 Database_controller::sql_query error ")+sql_str).c_str());
  313. }
  314. }
  315. catch (sql::SQLException &e)
  316. {
  317. /* Use what() (derived from std::runtime_error) to fetch the error message */
  318. giveback_connect_channel(tp_database_connect_channel);
  319. sprintf(buf, "# ERR: %s\n (MySQL error code: %d, SQLState: %s\nsql: %s", e.what(), e.getErrorCode(), e.getSQLState().c_str(), sql_str.c_str());
  320. return Error_manager(DB_UPDATE_FAILED, NEGLIGIBLE_ERROR, buf);
  321. }
  322. catch (std::runtime_error &e)
  323. {
  324. giveback_connect_channel(tp_database_connect_channel);
  325. sprintf(buf, "# ERR: %s\n ERR: runtime_error in %s \nsql: %s",e.what(),__FILE__, sql_str.c_str());
  326. return Error_manager(DB_UPDATE_FAILED, NEGLIGIBLE_ERROR, buf);
  327. }
  328. return SUCCESS;
  329. }
  330. Error_manager Database_controller::sql_control(std::string sql_str)
  331. {
  332. #ifdef COUT_SQL_STRING
  333. LOG(INFO) << " sql_str = "<< sql_str << " --- " << this;
  334. #endif
  335. Error_manager t_error;
  336. //huli
  337. t_error = check_status();
  338. if ( t_error != Error_code::SUCCESS )
  339. {
  340. return t_error;
  341. }
  342. boost::shared_ptr<Database_connect_channel> tp_database_connect_channel;
  343. t_error = occupancy_connect_channel(tp_database_connect_channel);
  344. if ( t_error != Error_code::SUCCESS )
  345. {
  346. return t_error;
  347. }
  348. char buf[1024];
  349. memset(buf, 0, 1024);
  350. try
  351. {
  352. tp_database_connect_channel->mp_connect_channel->setSchema(m_db_param.db_name);
  353. boost::scoped_ptr<sql::Statement> stmt(tp_database_connect_channel->mp_connect_channel->createStatement());
  354. stmt->execute(sql_str);
  355. giveback_connect_channel(tp_database_connect_channel);
  356. return SUCCESS;
  357. }
  358. catch (sql::SQLException &e)
  359. {
  360. giveback_connect_channel(tp_database_connect_channel);
  361. sprintf(buf, "# ERR: %s\n (MySQL error code: %d, SQLState: %s\nsql: %s", e.what(), e.getErrorCode(), e.getSQLState().c_str(), sql_str.c_str());
  362. return Error_manager(DB_CONTROL_FAILED, NEGLIGIBLE_ERROR, buf);
  363. }
  364. catch (std::runtime_error &e)
  365. {
  366. giveback_connect_channel(tp_database_connect_channel);
  367. sprintf(buf, "# ERR: %s\n ERR: runtime_error in %s \nsql: %s",e.what(),__FILE__, sql_str.c_str());
  368. return Error_manager(DB_CONTROL_FAILED, NEGLIGIBLE_ERROR, buf);
  369. }
  370. return SUCCESS;
  371. }
  372. void Database_controller::database_reconnect_thread()
  373. {
  374. //hili 这个用条件变量去控制
  375. while (m_database_reconnect_condition.is_alive())
  376. {
  377. m_database_reconnect_condition.wait_for_ex(std::chrono::seconds(1));
  378. if (m_database_reconnect_condition.is_alive())
  379. {
  380. updata_connect_channel();
  381. fill_up_connect_channel();
  382. }
  383. }
  384. }
  385. // 检查连接状态
  386. Error_manager Database_controller::updata_connect_channel()
  387. {
  388. for (auto iter = m_database_connect_channel_list.begin(); iter != m_database_connect_channel_list.end();)
  389. {
  390. if ((*iter)->mp_connect_channel == nullptr)
  391. {
  392. iter = m_database_connect_channel_list.erase(iter);
  393. }
  394. else if ( (*iter)->m_occupancy_lock.try_lock() )
  395. {
  396. if( (*iter)->mp_connect_channel->isValid() )
  397. {
  398. (*iter)->m_occupancy_lock.unlock();
  399. ++iter;
  400. }
  401. else
  402. {
  403. (*iter)->m_occupancy_lock.unlock();
  404. iter = m_database_connect_channel_list.erase(iter);
  405. }
  406. }
  407. else
  408. {
  409. ++iter;
  410. }
  411. }
  412. return Error_code::SUCCESS;
  413. }
  414. ////创建补充 数据库连接的通路
  415. Error_manager Database_controller::fill_up_connect_channel()
  416. {
  417. if ( m_conn_pool_size <=0 )
  418. {
  419. return Error_manager(Error_code::DB_CONNECT_CHANNEL_NUMBER_ERROR, Error_level::MINOR_ERROR,
  420. "数据库连接通道数量错误 Database_controller::fill_up_connect_channel() error ");
  421. }
  422. // 填充连接池
  423. int retry_count = DB_RECONNECTION_NUMBER;
  424. //胡力 建议修改如下
  425. while (m_database_connect_channel_list.size() < m_conn_pool_size && retry_count > 0)
  426. {
  427. try
  428. {
  429. //胡力 数据库连接的指针 建议改名 tp_sql_connnect
  430. boost::shared_ptr<sql::Connection> tp_connect_channel(sql::mysql::get_driver_instance()->connect(m_db_param.connect_string, m_db_param.username, m_db_param.pass));
  431. if (tp_connect_channel != nullptr && tp_connect_channel->isValid())
  432. {
  433. boost::shared_ptr<Database_connect_channel> tp_database_connect_channel(new Database_connect_channel);
  434. tp_database_connect_channel->mp_connect_channel = tp_connect_channel;
  435. // tp_database_connect_channel->m_occupancy_lock.unlock();
  436. m_database_connect_channel_list.push_back(tp_database_connect_channel);
  437. }
  438. else
  439. {
  440. retry_count--;
  441. }
  442. }
  443. catch (sql::SQLException &e)
  444. {
  445. char buf[1024];
  446. memset(buf, 0, 1024);
  447. sprintf(buf, "# ERR: %s\n (MySQL error code: %d, SQLState: %s", e.what(), e.getErrorCode(), e.getSQLState().c_str());
  448. retry_count--;
  449. }
  450. }
  451. if ( retry_count <= 0 )
  452. {
  453. m_database_controller_status = E_DISCONNECT;
  454. //胡力 如果报错 队列中部分成功要回收处理 需要讨论!!!!!!!!!!!!!!!!!!
  455. m_database_connect_channel_list.clear();
  456. return Error_manager(Error_code::DB_CONNECT_FAILED, Error_level::MINOR_ERROR,
  457. "数据库连接失败 Database_controller::fill_up_connect_channel() error ");
  458. }
  459. else
  460. {
  461. m_database_controller_status = E_READY;
  462. return Error_code::SUCCESS;
  463. }
  464. return SUCCESS;
  465. }
  466. //zhan you jieyong
  467. Error_manager Database_controller::occupancy_connect_channel(boost::shared_ptr<Database_connect_channel> & p_database_connect_channel)
  468. {
  469. for (auto iter = m_database_connect_channel_list.begin(); iter != m_database_connect_channel_list.end(); ++iter)
  470. {
  471. if ( (*iter)->mp_connect_channel != nullptr)
  472. {
  473. if ( (*iter)->m_occupancy_lock.try_lock() )
  474. {
  475. if ( (*iter)->mp_connect_channel->isValid() )
  476. // if ( !(*iter)->mp_connect_channel->isClosed() )
  477. {
  478. p_database_connect_channel = (*iter);
  479. return Error_code::SUCCESS;
  480. }
  481. (*iter)->m_occupancy_lock.unlock();
  482. }
  483. }
  484. //
  485. //
  486. //
  487. // if ( (*iter)->mp_connect_channel != nullptr && (*iter)->mp_connect_channel->isValid() )
  488. // {
  489. // if ( (*iter)->m_occupancy_lock.try_lock() )
  490. // {
  491. // p_database_connect_channel = (*iter);
  492. // return Error_code::SUCCESS;
  493. // }
  494. //
  495. // }
  496. }
  497. //if not find
  498. return Error_manager(Error_code::DB_CONNECT_CHANNEL_NOT_FOUND, Error_level::MINOR_ERROR,
  499. "数据库连接通道未找到 Database_controller::occupancy_connect_channel error ");
  500. }
  501. //guihuan
  502. Error_manager Database_controller::giveback_connect_channel(boost::shared_ptr<Database_connect_channel> p_database_connect_channel)
  503. {
  504. if ( p_database_connect_channel == nullptr )
  505. {
  506. return Error_manager(Error_code::POINTER_IS_NULL, Error_level::MINOR_ERROR,
  507. "指针为空 Database_controller::giveback_connect_channel error ");
  508. }
  509. p_database_connect_channel->m_occupancy_lock.unlock();
  510. return Error_code::SUCCESS;
  511. }