mysql 自动重连
来源:互联网 发布:记录纪念日的软件 编辑:程序博客网 时间:2024/04/30 04:14
MYSQL_OPT_RECONNECT (argument type: my_bool *)
Enable or disable automatic reconnection to the server if the connection is found to have been lost. Reconnect has been off by default since MySQL 5.0.3; this option is new in 5.0.13 and provides a way to set reconnection behavior explicitly.
#如果连接丢失,启用或禁用自动重连到Serve,。自5.0.3之后,重连默认被禁用。这个选项是5.0.13新增的,提供了明确设置重连行为的选项。
Note: mysql_real_connect() incorrectly reset the MYSQL_OPT_RECONNECT option to its default value before MySQL 5.0.19. Therefore, prior to that version, if you want reconnect to be enabled for each connection, you must call mysql_options() with the MYSQL_OPT_RECONNECT option after each call to mysql_real_connect(). This is not necessary as of 5.0.19: Call mysql_options() only before mysql_real_connect() as usual.
#注意:mysql_real_connect() 在5.0.19之前错误地重置了MYSQL_OPT_RECONNECT选项为默认值(不启用),因此,在5.0.19之前,如果你希望为连接启用重连,你必须在mysql_real_connect()之后调用mysql_options来设置MYSQL_OPT_RECONNECT。5.0.19之后就不需要这样了,只需要(跟其他选项一样)在mysql_real_connect之前调用即可。
Enable or disable automatic reconnection to the server if the connection is found to have been lost. Reconnect has been off by default since MySQL 5.0.3; this option is new in 5.0.13 and provides a way to set reconnection behavior explicitly.
#如果连接丢失,启用或禁用自动重连到Serve,。自5.0.3之后,重连默认被禁用。这个选项是5.0.13新增的,提供了明确设置重连行为的选项。
Note: mysql_real_connect() incorrectly reset the MYSQL_OPT_RECONNECT option to its default value before MySQL 5.0.19. Therefore, prior to that version, if you want reconnect to be enabled for each connection, you must call mysql_options() with the MYSQL_OPT_RECONNECT option after each call to mysql_real_connect(). This is not necessary as of 5.0.19: Call mysql_options() only before mysql_real_connect() as usual.
#注意:mysql_real_connect() 在5.0.19之前错误地重置了MYSQL_OPT_RECONNECT选项为默认值(不启用),因此,在5.0.19之前,如果你希望为连接启用重连,你必须在mysql_real_connect()之后调用mysql_options来设置MYSQL_OPT_RECONNECT。5.0.19之后就不需要这样了,只需要(跟其他选项一样)在mysql_real_connect之前调用即可。
也就是说,不管是哪个版本,希望启用MYSQL_OPT_RECONNECT,都应该调用mysql_options()明确设置它。由于一个BUG,5.0.19之前必须在mysql_real_connect()之后设置,而更新的版本则可以在mysql_real_connect之前设置。
这里的坑是,在没有设置了RECONNECT时,mysql_ping()并不会自动重连。
因此最完整的解决方法是:
1. 在使用mysql_real_connect连接数据库之后,再使用mysql_options( &mysql, MYSQL_OPT_RECONNECT, … ) 来设置为自动重连。这样当mysql连接丢失的时候,使用mysql_ping能够自动重连数据库。
示例代码:
mysql_init() ...
mysql_real_connect()...
char value = 1;
mysql_options(&mysql, MYSQL_OPT_RECONNECT, (char *)&value);
mysql_real_connect()...
char value = 1;
mysql_options(&mysql, MYSQL_OPT_RECONNECT, (char *)&value);
2. 在执行查询的之前使用mysql_ping()确保自动重连。(但是这个是必要的吗?找不到相应的说明。。。)
------------------------------------------------------------
经过测试,设置handler为自动重连,每次执行查询之前 mysql_ping 保证连接未断开,但未成功,mysql_ping 未进行自动重连,还是建议检查mysql_ping 返回值,主动进行重连吧。。。
------------------------------------------------------------
0 0
- mysql 自动重连
- hibernate连接mysql自动重连
- mysql控制自动重连行为
- 自动重连
- asmack自动重连
- 数据库自动重连
- TCP自动重连
- jboss下mysql数据源自动重连的配置备忘
- mysql 连接闪断自动重连的方法
- JDBC如何实现Mysql自动重连的机制
- PHP PDO 自动重连
- activeMQ 自动重连机制
- hibernate 断开自动重连
- dbcp自动重连机制
- 夜神自动重连
- mysql重连的问题
- mysql断线重连设置
- mysql重连,连接丢失
- 行人检测(Pedestrian Detection)小结-Part I
- sql 所有连接
- 本地双Tomcat存在问题汇总
- pureftp读取目录列表失败解决
- 利用iptables开放被动模式下的FTP服务 2
- mysql 自动重连
- windows网络命令大全
- 创建hash表的注意事项
- 用OpenSceneGraph实现的NeHe OpenGL教程 - 第三十五课_附录1
- 程序压力测试
- 15个顶级Java多线程面试题及回答
- Flash AS3 性能优化注意点
- Android 读取联系人2.0以上
- x264 - FENC_SRIDE、FDEC_STRIDE相关以及fenc、fdec的buf存储结构