- MySQL jdbc timeout">1 . MySQL jdbc timeout
- 2. Jedis timeout
1 . MySQL jdbc timeout
查阅MySQL Connector/J 5.1 Developer Guide 中的jdbc配置参数,有
connectTimeout Timeout for socket connect (in milliseconds), with 0 being no timeout. Only works on JDK-1.4 or newer. Defaults to ‘0’. Default: 0 Since version: 3.0.1 |
---|
socketTimeout Timeout on network socket operations (0, the default means no timeout). Default: 0 Since version: 3.0.1 |
这两个参数分别就是对应上面我们分析的connect timeout和so timeout。
参数的设置方法有两种,一种是通过url设置,
jdbc:mysql://[host1][:port1][,[host2][:port2]]...[/[database]]
[?propertyName1=propertyValue1[&propertyName2=propertyValue2]...]
即在url后面通过?加参数,比如jdbc:mysql://192.168.1.1:3306/test?connectTimeout=2000&socketTime=2000
还有一种方式是:
Properties info = new Properties();
info.put("user", this.username);
info.put("password", this.password);
info.put("connectTimeout", "2000");
info.put("socketTime", "2000");
return DriverManager.getConnection(this.url, info);
2. Jedis timeout
Jedis是最流行的redis java客户端工具,redis.clients.jedis.Jedis对象的构造器中就有参数设置,
public Jedis(final String host, final int port, final int connectionTimeout,
final int soTimeout) {
super(host, port, connectionTimeout, soTimeout);
}
// 用一个参数timeout同时设置connect timeout 和 so timeout
public Jedis(final String host, final int port, final int timeout) {
super(host, port, timeout);
}
Jedis中so timeout个人觉得是有比较重要意义的,首先jedis so timeout默认值为2000毫秒,jedis的操作流程是客户端发送命令给客户端执行,然后客户端就开始执行InputStream.read()读取响应,当某个命令比较耗时(比如数据非常多的情况下执行“keys *”),而导致客户端迟迟没有收到响应,就可能导致java.net.SocketTimeoutException: Read timed out异常抛出。一般是不建议客户端执行非常耗时的命令,但是也不排除有这种特殊逻辑,那这时候就有可能需要修改Jeids中这个so timeout的值。