10/22/13

mysql++-2.1.1 源码修改记录

1、lib/connection.h 添加ping方法声明

int ping(const char* db, const char* host = "",
const char* user = "", const char* passwd = "",
uint port = 0, my_bool compress = 0,
unsigned int connect_timeout = 60, cchar* socket_name = 0,
unsigned int client_flag = 0);

2、lib/connection.cpp ping方法体

int
Connection::ping(const char* db, const char* host,
                const char* user, const char* passwd, uint port,
                my_bool compress, unsigned int connect_timeout,
                cchar* socket_name, unsigned int client_flag)
{
        if (connected()) {
                if(mysql_ping(&mysql_)){
                        mysql_init(&mysql_);
                        if (connect(db, host, user, passwd, port, compress,
                           connect_timeout, socket_name, client_flag)) {
                                unlock();
                                success_ = is_connected_ = true;
                        }
                        else {
                                unlock();
                                success_ = is_connected_ = false;
                                if (throw_exceptions()) {
                                        throw ConnectionFailed(error());
                                }
                        }
                        return success_ ? 0:1;
                }else{
                        return 0;
                }
        }
        else {
                mysql_init(&mysql_);
                if (connect(db, host, user, passwd, port, compress,
                   connect_timeout, socket_name, client_flag)) {
                        unlock();
                        success_ = is_connected_ = true;
                }
                else {
                        unlock();
                        success_ = is_connected_ = false;
                        if (throw_exceptions()) {
                                throw ConnectionFailed(error());
                        }
                }
                return success_ ? 0:1;
        }
}

3、编译安装

        # ./configure
        $ make
        $ su
        # make install

4、应用代码修改

old:this->con->ping() != 0  //无重连机制

new:this->con->ping(DB,HOST, USER, PASSWD, PORT) != 0

代码添加了数据库重连 。