mysql ping 长连接超时时间_如何用MySQL ++设置mysql_ping超时

bd96500e110b49cbb3cd949968f18be7.png

I want mysql_ping to timeout in matter of seconds. In the testcase below if a connection is established followed by "ifconfig eth0 down" the false side of conn.ping() is never reached. The thread/process enters what seems like an endless wait. I want to change this and make it timeout faster, preferably using MySQL++ options. Any idea which option that might be ?

Maybe I also need to set how many times it will retry ?

Reading up on the documentation for MySQL, it tells us mysql_ping will try automatic reconnect. Which is fine but eventually I'd want that to timeout aswell.

http://dev.mysql.com/doc/refman/5.1/en/mysql-options.html

http://dev.mysql.com/doc/refman/5.1/en/mysql-ping.html

Since it does automatic reconnect, MYSQL_OPT_CONNECT_TIMEOUT seems like the way to go. However in the testcase below setting mysqlpp::ConnectTimeoutOption(1) does not help.

Seems like MySQL++ ping() is just a wrapper around the C API, i gathered this from "/usr/include/mysql++/dbdriver.h" which defines ping() as:

bool ping() { return !mysql_ping(&mysql_); }

Versions of the libraries I'm using are:

libmysql++-dev: 3.0.9-1+b1

libmysql++3: 3.0.9-1+b1

mysql-client-5.1: 5.1.49-3

#include

#include

//g++ -o test -I/usr/include/mysql/ -lmysqlpp testcase_mysql_timeout.cpp

int main(int argc, char *argv[]) {

mysqlpp::Connection conn;

conn = mysqlpp::Connection(true);

try {

conn.set_option(new mysqlpp::MultiStatementsOption(true));

conn.set_option(new mysqlpp::ConnectTimeoutOption(1));

conn.set_option(new mysqlpp::InteractiveOption(true));

}

catch (mysqlpp::BadOption &e) {

std::cerr << "ConnectDB exception: " << e.what() << std::endl;

}

conn.connect("MyDB", "my.dyndns.org", "user", "password", 3306);

while(1) {

if (!conn.ping()) {

std::cout << "Host not reachable. Try to reconnect?" << std::endl;

}

else {

std::cout << "Host is reachable all is good." << std::endl;

}

usleep(1000000);

}

}

解决方案

I'm afraid I have to answer my own question :)

Reading a bit more of the MySQL documentation gave the answer. Setting the connect timeout in this case does very little. It still has to wait for TCP/IP Close_Wait_Timeout with a default of 10 minutes:

MYSQL_OPT_READ_TIMEOUT (argument type: unsigned int *)

The timeout in seconds for attempts to read from the server. Each attempt uses this timeout value and there are retries if necessary, so the total effective timeout value is three times the option value. You can set the value so that a lost connection can be detected earlier than the TCP/IP Close_Wait_Timeout value of 10 minutes. Before MySQL 5.1.41, this option applies only to TCP/IP connections and, prior to MySQL 5.1.12, only for Windows.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值