| | |
| | | #pragma once |
| | | #include "MysqlConn.h" |
| | | #include "./tinyxml/tinyxml.h" |
| | | |
| | | #include <queue> |
| | | #include <mutex> |
| | | #include <thread> |
| | | #include <atomic> |
| | | #include <condition_variable> |
| | | //#include <json/json.h> |
| | | |
| | | #include "MysqlConn.h" |
| | | |
| | | #pragma comment(lib, "./tinyxml/x64/Debug/tinyxml.lib") |
| | | //using namespace Json; |
| | | |
| | | // åºç¨-å便¨¡å¼ï¼ææ±æ¨¡å¼[éè¦èèå¤çº¿ç¨å®å
¨é®é¢] |
| | | class ConnectionPool |
| | | { |
| | | private: |
| | | ConnectionPool(); |
| | | // ç§»å¨æ·è´æç»è¿æ¯æä¸ä»
æä¸ä¸ªå¯¹è±¡ï¼æä»¥ä¾æ§æ¯å±äºå便¨¡å¼ã |
| | | // delete 黿¢æ·è´æé åæ·è´èµå¼çç±»å¯¹è±¡çæ |
| | | ConnectionPool(ConnectionPool&) = delete; |
| | | ConnectionPool& operator=(ConnectionPool&) = delete; |
| | | ~ConnectionPool(); |
| | | // è§£æxmlé
ç½®æä»¶ è¯»åæ°æ®åºåè¿æ¥æ± çç¸å
³ä¿¡æ¯ |
| | | bool parseXmlFile(); |
| | | // æ·»å è¿æ¥ |
| | | bool addConnection(); |
| | | // 线ç¨å½æ° |
| | | void productConnection(); |
| | | void recycleConnection(); |
| | | |
| | | // åæ¾æ°æ®åºè¿æ¥æ± 建ç«çè¿æ¥ |
| | | queue<MysqlConn*> m_connections; |
| | | // ä¸äºåºæ¬ä¿¡æ¯ |
| | | string m_ip; // IP |
| | | unsigned short m_port; // ç«¯å£ |
| | | string m_user; // ç¨æ·å |
| | | string m_passwd; // å¯ç |
| | | string m_dbName; // æ°æ®åºåç§° |
| | | int m_minSize; // åå§è¿æ¥é(æå°è¿æ¥é) |
| | | int m_maxSize; // æå¤§è¿æ¥é |
| | | int m_timeout; // è¶
æ¶æ¶é¿ |
| | | int m_maxIdleTime; // æå¤§ç©ºé²æ¶é¿ |
| | | // 线ç¨å®å
¨ç¸å
³ |
| | | mutex m_mutex; |
| | | condition_variable m_cond; // ä»
ç¨ä¸ä¸ªæ¡ä»¶å鿥å¤é线ç¨ï¼ä½å¹¶ä¸å½±å线ç¨è¿è¡ |
| | | condition_variable m_cond1; |
| | | // è¿æ¥æ°é |
| | | atomic_int m_num; // è¿æ¥çæ»æ°é |
| | | public: |
| | | // è·ååä¾å¯¹è±¡çæ¥å£ |
| | | static ConnectionPool* getConnectPool(); |
| | | // ç¨æ·è·åè¿æ¥çæ¥å£, 妿è·å失败ï¼ä¼è¿ånullptr |
| | | shared_ptr<MysqlConn> getConnection(); |
| | | static ConnectionPool* getInstance( ); |
| | | |
| | | std::shared_ptr<MysqlConn> getMysqlConn(); // 仿°æ®åºè¿æ¥æ± è·åè¿æ¥ |
| | | |
| | | ConnectionPool(const ConnectionPool& other) = delete; |
| | | ConnectionPool& operator = (const ConnectionPool & other ) = delete; |
| | | |
| | | ~ConnectionPool(); |
| | | |
| | | void description(); |
| | | |
| | | protected: |
| | | ConnectionPool( ); // æé 彿° |
| | | private: |
| | | bool parseJsonFile(); // è§£æé
ç½® |
| | | |
| | | void produce(); |
| | | void recycle(); |
| | | void addConnection(); // å¢å è¿æ¥æ° |
| | | |
| | | std::string m_ip; |
| | | std::string m_userName; |
| | | std::string m_passwd; |
| | | std::string m_db; |
| | | unsigned short m_port; |
| | | |
| | | int m_max_conn; |
| | | int m_min_conn; |
| | | |
| | | int m_timeout; // è¿æ¥è¶
æ¶æ¶é´ |
| | | int max_del_time; // æå¤§å 餿¶é´( è¿æ¥ç©ºé²æ¶é´è¶
è¿è¿ä¸ª,å°±ç»å½åè¿æ¥å
³é ) |
| | | std::queue<MysqlConn*>m_connkQueue ; // è¿æ¥éå |
| | | std::mutex m_mutex; // äºæ¥é |
| | | std::condition_variable m_cond; // æ¡ä»¶åé |
| | | }; |
| | | |