MySQLConnectionPool.h 3.2 KB
Newer Older
Z
zhiru 已提交
1 2 3 4
#include "mysql++/mysql++.h"

#include <string>
#include <unistd.h>
Z
update  
zhiru 已提交
5
#include <atomic>
Z
zhiru 已提交
6

7 8
#include "Log.h"

Z
zhiru 已提交
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
class MySQLConnectionPool : public mysqlpp::ConnectionPool {

public:
    // The object's only constructor
    MySQLConnectionPool(std::string dbName,
                        std::string userName,
                        std::string passWord,
                        std::string serverIp,
                        int port = 0,
                        int maxPoolSize = 8) :
                        db_(dbName),
                        user_(userName),
                        password_(passWord),
                        server_(serverIp),
                        port_(port),
Z
update  
zhiru 已提交
24
                        max_pool_size_(maxPoolSize)
Z
zhiru 已提交
25 26 27 28
                        {

        conns_in_use_ = 0;

Z
update  
zhiru 已提交
29
        max_idle_time_ = 10; //10 seconds
Z
zhiru 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43
    }

    // The destructor.  We _must_ call ConnectionPool::clear() here,
    // because our superclass can't do it for us.
    ~MySQLConnectionPool() override {
        clear();
    }

    // Do a simple form of in-use connection limiting: wait to return
    // a connection until there are a reasonably low number in use
    // already.  Can't do this in create() because we're interested in
    // connections actually in use, not those created.  Also note that
    // we keep our own count; ConnectionPool::size() isn't the same!
    mysqlpp::Connection* grab() override {
Z
update  
zhiru 已提交
44
        while (conns_in_use_ > max_pool_size_) {
Z
zhiru 已提交
45 46 47 48 49 50 51 52 53 54
            sleep(1);
        }

        ++conns_in_use_;
        return mysqlpp::ConnectionPool::grab();
    }

    // Other half of in-use conn count limit
    void release(const mysqlpp::Connection* pc) override {
        mysqlpp::ConnectionPool::release(pc);
Z
update  
zhiru 已提交
55

56
        if (conns_in_use_ <= 0) {
Z
update  
zhiru 已提交
57
            ENGINE_LOG_WARNING << "MySQLConnetionPool::release: conns_in_use_ is less than zero.  conns_in_use_ = " << conns_in_use_ << std::endl;
58
        }
59 60 61
        else {
            --conns_in_use_;
        }
Z
zhiru 已提交
62 63
    }

Z
update  
zhiru 已提交
64 65 66 67
    int getConnectionsInUse() {
        return conns_in_use_;
    }

Z
zhiru 已提交
68
    void set_max_idle_time(int max_idle) {
Z
update  
zhiru 已提交
69
        max_idle_time_ = max_idle;
Z
zhiru 已提交
70 71
    }

Z
update  
zhiru 已提交
72 73 74 75
    std::string getDB() {
        return db_;
    }

Z
zhiru 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
protected:

    // Superclass overrides
    mysqlpp::Connection* create() override {
        // Create connection using the parameters we were passed upon
        // creation.
        mysqlpp::Connection* conn = new mysqlpp::Connection();
        conn->set_option(new mysqlpp::ReconnectOption(true));
        conn->connect(db_.empty() ? 0 : db_.c_str(),
                      server_.empty() ? 0 : server_.c_str(),
                      user_.empty() ? 0 : user_.c_str(),
                      password_.empty() ? 0 : password_.c_str(),
                      port_);
        return conn;
    }

    void destroy(mysqlpp::Connection* cp) override {
        // Our superclass can't know how we created the Connection, so
        // it delegates destruction to us, to be safe.
        delete cp;
    }

    unsigned int max_idle_time() override {
Z
update  
zhiru 已提交
99
        return max_idle_time_;
Z
zhiru 已提交
100 101 102 103
    }

private:
    // Number of connections currently in use
Z
update  
zhiru 已提交
104
    std::atomic<int> conns_in_use_;
Z
zhiru 已提交
105 106 107 108 109

    // Our connection parameters
    std::string db_, user_, password_, server_;
    int port_;

Z
update  
zhiru 已提交
110
    int max_pool_size_;
Z
zhiru 已提交
111

Z
update  
zhiru 已提交
112
    unsigned int max_idle_time_;
Z
zhiru 已提交
113
};