mysql++ Connect

mysqlpp:: Connect類型主要負責鏈接事宜mysql

mysqlpp::OpeitonalExceptionsios

查看源碼發現OptionalExceptions就是一個表示「是否須要拋出異常」的變量的包裝。在Connection類型的內容,會在出現錯誤的時候調用OpetionalExceptions.throw_exceptions( )方法來查看是否須要使用異常的手段來表示錯誤。sql

 

mysqlpp::Connection數據庫

這個類型是用戶程序可以看到的少數幾個類型,它所包含的主要的方法就是「鏈接/斷開」,「建立/刪除/選擇數據庫",」查看table數據行數「,「建立Query對象」,」關閉mysql服務等操做「。函數

該類能夠返回一個mysqlpp::Query類型,主要負責查詢操做。另外,查看mysqlpp::Query源碼發現它的構造函數一定須要一個mysqlpp::Connection,也就是說mysqlpp::Query的全部操做,其實也就是再次調用mysqlpp::Connection的對應方法,在由Connection調用mysqlpp::DBDriver來處理操做。性能

 

鏈接池spa

對於一個簡單的數據庫應用,對於數據庫的訪問不是很頻繁,這時在須要訪問數據庫時,新建立一個鏈接,用完後就關閉它,這樣作也不會帶來什麼明顯的性能上的開銷。可是對於一個複雜的數據庫應用,狀況就徹底不一樣了,頻繁的創建、關閉鏈接,會極大的減低系統的性能,由於對於鏈接的使用成了系統性能的瓶頸。數據庫鏈接池的基本原理是在內部對象池中維護必定數量的數據庫鏈接,並對外暴露數據庫鏈接獲取和返回方法。用戶使用完後將鏈接返回,此時鏈接並無關閉,而是由鏈接池管理器回收,併爲下一次使用作好準備。指針

數據庫鏈接池負責分配、管理和釋放數據庫鏈接,它容許應用程序重複使用一個現有的數據庫鏈接,而不是再從新創建一個;釋放空閒時間超過最大空閒時間的數據庫鏈接來避免由於沒有釋放數據庫鏈接而引發的數據庫鏈接遺漏。。這項技術能明顯提升對數據庫操做的性能。code

 mysqlpp::ConnectionPoolserver

此類主要維護一個雙向鏈表std::list<ConnectionInfo> PoolT,結構體內容包含:鏈接對象,是否在使用,上次使用時間,既然是鏈表就很容易的知道鏈接池當前的鏈接數及對鏈接增長刪除操做

virtual Connection* exchange(const Connection* pc); //

virtual Connection* grab();              //從鏈接池獲取一個鏈接,若是沒有則建立一個

Connection* safe_grab();                //從鏈接池獲取一個可用的鏈接

virtual void release(const Connection* pc);      //將一個鏈接置爲未使用狀態

void shrink()                      //從鏈接池刪除未使用的鏈接

 

鏈接池的使用

mysqlpp::ScopedConnection

經過構造函數從鏈接池中獲取一個Connection,析構函數將Connection對象從新放到鏈接池。

此類中維護了一個指向Connection指針,能夠執行Connection的操做

 

MysqlConnectPool.h

#ifndef _MYSQL_CONNECT_POOL_H_
#define _MYSQL_CONNECT_POOL_H_

#include <mysql++/mysql++.h>
#include <string>

class MysqlConnectPool : public mysqlpp::ConnectionPool
{
public:
    MysqlConnectPool(std::string dbname,
                    std::string serverip,
                    std::string user,
                    std::string passwd,
                    int port,
                    std::string charset)
        :m_dbname(dbname)
        ,m_server_ip(serverip)
        ,m_user(user)
        ,m_password(passwd)
        ,m_charset(charset)
        ,m_port(port)
    {
        m_max_idle_time = 300;
    }

    virtual ~MysqlConnectPool()
    {
        clear();
    }

    const std::string& getDBName() const { return m_dbname; }
    const std::string& getServerIP() const { return m_server_ip; }
    const std::string& getUser() const { return m_user; }
    const std::string& getPassword() const { return m_password; }
    const std::string& getCharset() const { return m_charset; }
    int getPort() const { return m_port; }

    void setMaxIdleTime(int max_idle)
    {
        m_max_idle_time = max_idle;
    }

    virtual mysqlpp::Connection* grab()
    {
        return mysqlpp::ConnectionPool::grab();
    }

    virtual void release(const mysqlpp::Connection* pc)
    {
        mysqlpp::ConnectionPool::release(pc);
    }

protected:
    virtual mysqlpp::Connection* create()
    {
        mysqlpp::Connection* conn = new mysqlpp::Connection(true);
        mysqlpp::SetCharsetNameOption* pOpt = new  mysqlpp::SetCharsetNameOption(m_charset.c_str());
        conn->set_option( pOpt );

        conn->connect(m_dbname.empty() ? 0 : m_dbname.c_str(),
                  m_server_ip.empty() ? 0 : m_server_ip.c_str(),
                  m_user.empty() ? 0 : m_user.c_str(),
                  m_password.empty() ? "" : m_password.c_str(),
                  m_port);

        return conn;
    }

    virtual void destroy(mysqlpp::Connection* cp)
    {
        delete cp;
    }

    virtual unsigned int max_idle_time()
    {
        return m_max_idle_time;
    }

private:
    std::string m_dbname;
    std::string m_server_ip;
    std::string m_user;
    std::string m_password;
    std::string m_charset;
    int         m_port;
    int         m_max_idle_time;
};

#endif
#include <memory>
#include <mysql++/ssqls.h>
#include <iostream>
#include <fstream>
#include <vector>
#include <iostream>
#include <iostream>

#include "MysqlConnectPool.h"

std::shared_ptr<MysqlConnectPool> db_pool = nullptr;
std::string db_name = "test";
std::string db_ip = "127.0.0.1";
std::string db_user = "root";
std::string db_passwd = "123456";
int db_port = 3306;
std::string db_charset = "utf8";

int main()
{
    db_pool = std::make_shared<MysqlConnectPool>(db_name, db_ip, db_user, db_passwd, db_port, db_charset);

    for (int i = 0; i < 10; i++)
    {
        try {
            mysqlpp::ScopedConnection conn(*db_pool);
            mysqlpp::Query query = conn->query();

            //................................
        }
        catch (const mysqlpp::Exception& e) {
            std::cout << "error:" << e.what() << "\n";
            return false;
        }
    }

    return 0;
}
相關文章
相關標籤/搜索