MySQLConnectionPool.h 2.6 KB
Newer Older
J
jinhai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.


Z
zhiru 已提交
19 20 21 22
#include "mysql++/mysql++.h"

#include <string>
#include <unistd.h>
Z
update  
zhiru 已提交
23
#include <atomic>
Z
zhiru 已提交
24

S
starlord 已提交
25
#include "utils/Log.h"
26

Z
update  
zhiru 已提交
27 28 29 30 31
namespace zilliz {
namespace milvus {
namespace engine {
namespace meta {

Z
zhiru 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
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 已提交
47
                        max_pool_size_(maxPoolSize) {
Z
zhiru 已提交
48 49 50

        conns_in_use_ = 0;

Z
update  
zhiru 已提交
51
        max_idle_time_ = 10; //10 seconds
Z
zhiru 已提交
52 53 54 55 56 57 58 59
    }

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

Z
update  
zhiru 已提交
60
    mysqlpp::Connection *grab() override;
Z
zhiru 已提交
61 62

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

S
starlord 已提交
65 66 67
//    int getConnectionsInUse();
//
//    void set_max_idle_time(int max_idle);
Z
zhiru 已提交
68

Z
update  
zhiru 已提交
69
    std::string getDB();
Z
update  
zhiru 已提交
70

Z
zhiru 已提交
71 72 73
protected:

    // Superclass overrides
Z
update  
zhiru 已提交
74
    mysqlpp::Connection *create() override;
Z
zhiru 已提交
75

Z
update  
zhiru 已提交
76
    void destroy(mysqlpp::Connection *cp) override;
Z
zhiru 已提交
77

Z
update  
zhiru 已提交
78
    unsigned int max_idle_time() override;
Z
zhiru 已提交
79 80 81

private:
    // Number of connections currently in use
Z
update  
zhiru 已提交
82
    std::atomic<int> conns_in_use_;
Z
zhiru 已提交
83 84 85 86 87

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

Z
update  
zhiru 已提交
88
    int max_pool_size_;
Z
zhiru 已提交
89

Z
update  
zhiru 已提交
90
    unsigned int max_idle_time_;
Z
update  
zhiru 已提交
91 92 93 94 95 96
};

} // namespace meta
} // namespace engine
} // namespace milvus
} // namespace zilliz