Skip to content

Commit

Permalink
feat: tc_mysql init add connecttimeout/readwritetimeout
Browse files Browse the repository at this point in the history
  • Loading branch information
ruanshudong committed Sep 18, 2024
1 parent dd84c3f commit bccdeef
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
8 changes: 4 additions & 4 deletions util/include/util/tc_mysql.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,13 @@ struct TC_DBConf
int _flag;

/**
* 连接超时
* 连接超时(秒)
* Port
*/
int _connectTimeout;

/**
* 读写超时
* 读写超时(秒)
* Port
*/
int _writeReadTimeout;
Expand Down Expand Up @@ -209,7 +209,7 @@ class TC_Mysql
* @param iFlag 客户端标识
* @param iFlag Client Identity
*/
TC_Mysql(const string& sHost, const string& sUser = "", const string& sPasswd = "", const string& sDatabase = "", const string &sCharSet = "", int port = 0, int iFlag = 0);
TC_Mysql(const string& sHost, const string& sUser = "", const string& sPasswd = "", const string& sDatabase = "", const string &sCharSet = "", int port = 0, int iFlag = 0, int connectTimeout = 10, int writeReadTimeout = 0);

/**
* @brief 构造函数.
Expand Down Expand Up @@ -245,7 +245,7 @@ class TC_Mysql
* @return 无
* @return none
*/
void init(const string& sHost, const string& sUser = "", const string& sPasswd = "", const string& sDatabase = "", const string &sCharSet = "", int port = 0, int iFlag = 0);
void init(const string& sHost, const string& sUser = "", const string& sPasswd = "", const string& sDatabase = "", const string &sCharSet = "", int port = 0, int iFlag = 0, int connectTimeout = 10, int writeReadTimeout = 0);

/**
* @brief 初始化.
Expand Down
10 changes: 6 additions & 4 deletions util/src/tc_mysql.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ TC_Mysql::TC_Mysql()
_pstMql = mysql_init(NULL);
}

TC_Mysql::TC_Mysql(const string& sHost, const string& sUser, const string& sPasswd, const string& sDatabase, const string &sCharSet, int port, int iFlag)
TC_Mysql::TC_Mysql(const string& sHost, const string& sUser, const string& sPasswd, const string& sDatabase, const string &sCharSet, int port, int iFlag, int connectTimeout, int writeReadTimeout)
:_bConnected(false)
{
init(sHost, sUser, sPasswd, sDatabase, sCharSet, port, iFlag);
init(sHost, sUser, sPasswd, sDatabase, sCharSet, port, iFlag, connectTimeout, writeReadTimeout);

_pstMql = mysql_init(NULL);
}
Expand All @@ -57,7 +57,7 @@ TC_Mysql::~TC_Mysql()
}
}

void TC_Mysql::init(const string& sHost, const string& sUser, const string& sPasswd, const string& sDatabase, const string &sCharSet, int port, int iFlag)
void TC_Mysql::init(const string& sHost, const string& sUser, const string& sPasswd, const string& sDatabase, const string &sCharSet, int port, int iFlag, int connectTimeout, int writeReadTimeout)
{
_dbConf._host = sHost;
_dbConf._user = sUser;
Expand All @@ -66,6 +66,8 @@ void TC_Mysql::init(const string& sHost, const string& sUser, const string& sPas
_dbConf._charset = sCharSet;
_dbConf._port = port;
_dbConf._flag = iFlag;
_dbConf._writeReadTimeout = writeReadTimeout;
_dbConf._connectTimeout = connectTimeout;
}

void TC_Mysql::init(const TC_DBConf& tcDBConf)
Expand Down Expand Up @@ -526,7 +528,7 @@ void TC_Mysql::execute(const string& sSql)

if (iRet != 0)
{
throw TC_Mysql_Exception("[TC_Mysql::execute]: mysql_query: [ " + sSql+" ] :" + string(mysql_error(_pstMql)));
throw TC_Mysql_Exception("[TC_Mysql::execute]: mysql_query: [ " + sSql+" ] :" + string(mysql_error(_pstMql)) + ", errno:" + TC_Common::tostr(iRet));
}
}

Expand Down

0 comments on commit bccdeef

Please sign in to comment.