`
yymmiinngg
  • 浏览: 178163 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

mysql-connector-java-5.0.3.jar Bug之 Statement.setQueryTimeout(int seconds)

阅读更多

    我们都知道在java.sql.Statement接口规范中有这么一个方法,如下代码片段:

    /**
     * Sets the number of seconds the driver will wait for a 
     * <code>Statement</code> object to execute to the given number of seconds.
     * If the limit is exceeded, an <code>SQLException</code> is thrown. A JDBC
     * driver must apply this limit to the <code>execute</code>,
     * <code>executeQuery</code> and <code>executeUpdate</code> methods. JDBC driver
     * implementations may also apply this limit to <code>ResultSet</code> methods
     * (consult your driver vendor documentation for details).
     *
     * @param seconds the new query timeout limit in seconds; zero means 
     *        there is no limit
     * @exception SQLException if a database access error occurs, 
     * this method is called on a closed <code>Statement</code>
     *            or the condition seconds >= 0 is not satisfied
     * @see #getQueryTimeout
     */
    void setQueryTimeout(int seconds) throws SQLException;

  

    从这段代码中我们能看到“@param seconds the new query timeout limit in seconds; zero means there is no limit”,也就是说“seconds”设置查询的超时“秒”数,如果为0则无超时。

 

    然而在mysql-connector-java-5.0.3.jar的实现中却不是这么按照接口规范做的(但符合JAVA接口规则),请看下面的代码片段: 

/**
 * 这里是com.mysql.jdbc.Statement类中设置查询超时时长的实现
 */
public void setQueryTimeout(int seconds)  throws SQLException  {
    if (seconds < 0) {
        throw SQLError.createSQLException(Messages.getString("Statement.21"), "S1009");
    }

    this.timeout = seconds;
}

// 这一段是在com.mysql.jdbc.Statement类中使用超时时长的代码
if ((this.timeout != 0) && (locallyScopedConn.versionMeetsMinimum(5, 0, 0))) {
      timeoutTask = new CancelTask(this);
      Connection.getCancelTimer().schedule(timeoutTask, this.timeout);
}

 

    从以上的代码可以看出setQueryTimeout方法设置的this.timeout 的值被Connection.getCancelTimer().schedule(timeoutTask, this.timeout);直接应用,而没进行任何的单位转换。

 

    于是我们如果设置想设置10秒的查询超时就必需写成setQueryTimeout(10000),而之前我一直是按照接口的规范写成setQueryTimeout(10) 。

 

    起初我数据库的数据量非常少,而随着时间的推移,数据量会越来越多,这时数据库查询的响应速度就会随之变慢,所以经过一段时间后,我就发现【com.mysql.jdbc.exceptions.MySQLTimeoutException: Statement cancelled due to timeout or client这种错误越来越多,大家应该很清楚是什么原因造成的吧?之前的数据库响应在10毫秒内没问题,因为数据量小,但数据量多了,于是响应就慢了,这时这种异常就多了,甚至到最后根本无法返回正常的查询结果。

 

    经过分析得出结论:这是mysql-connector-java-5.0.3.jar的严重BUG。

 

    对于这个BUG的解决方法就是使用mysql-connector-java-5.0.8-bin.jar或更高版本的JAR包。 

2
1
分享到:
评论

相关推荐

    mysql-connector-java-5.0.3.zip

    my-sql-driver mysql-connector-java-5.0.3.zip

    mysql-connector-java-5.0.3.jar

    java 的 mysql jdbc驱动:mysql-connector-java-5.0.3.jar

    woodstox-core-5.0.3.jar

    woodstox-core-5.0.3.jar

    asm-5.0.3.jar

    asm-5.0.3.jarasm-5.0.3.jar

    mysql-connector-java-5.x-bin.ra

    mysql-connector-java-5.0.3-bin.jar mysql-connector-java-5.1.18-bin.jar 两个版本,测试都可用。

    woodstox-core-5.0.3-API文档-中英对照版.zip

    赠送原API文档:woodstox-core-5.0.3-javadoc.jar; 赠送源代码:woodstox-core-5.0.3-sources.jar; 赠送Maven依赖信息文件:woodstox-core-5.0.3.pom; 包含翻译后的API文档:woodstox-core-5.0.3-javadoc-API文档...

    asm-util-5.0.3.jar

    asm-util-5.0.3.jarasm-util-5.0.3.jarasm-util-5.0.3.jar

    spring-cloud-starter-gateway下hibernate-validator-5.0.3.Final.jar

    spring-cloud-starter-gateway下的依赖hibernate-validator-5.0.3.Final.jar

    mysql-connector-java-5.0.3-bin.rar

    mysql-connector-java-5.0.3-bin.rar(mysql驱动)

    asm-analysis-5.0.3.jar

    asm-analysis-5.0.3.jar;asm-analysis-5.0.3.jar;asm-analysis-5.0.3.jar

    httpclient5-5.0.3.jar

    这是我一个抠图程序发送请求时专用的jar,maven仓库不好下载,我就上传到csdn,以便下次可以继续使用

    asm-commons-5.0.3.jar

    asm-commons-5.0.3.jarasm-commons-5.0.3.jarasm-commons-5.0.3.jar

    httpclient5-fluent-5.0.3.jar

    这是我一个抠图程序发送请求时专用的jar,maven仓库不好下载,我就上传到csdn,以便下次可以继续使用

    xz-5.0.3.tar.bz2

    页面下载xz-5.0.3.tar.bz2解压工具,然后编译安装 tar -xf xz-5.0.3.tar cd xz-5.0.3 ./configure make make install 这样就可以用xz命令来解压tar.xz格式文件, xz -d linux-3.1-rc4.tar.xz tar -xvf linux-3.1-rc...

    lettuce-core-5.0.3.RELEASE-API文档-中英对照版.zip

    赠送jar包:lettuce-core-5.0.3.RELEASE.jar; 赠送原API文档:lettuce-core-5.0.3.RELEASE-javadoc.jar; 赠送源代码:lettuce-core-5.0.3.RELEASE-sources.jar; 赠送Maven依赖信息文件:lettuce-core-5.0.3....

    asm-5.0.3.jar.zip

    java jar包,亲测试可用 安全,可以对其进行数字签名,只让能够识别数字签名的用户使用里面的东西。 加快下载速度; 压缩,使文件变小,与ZIP压缩机制完全相同。 包封装。能够让JAR包里面的文件依赖于统一版本的...

    lettuce-core-5.0.3.RELEASE-API文档-中文版.zip

    赠送jar包:lettuce-core-5.0.3.RELEASE.jar; 赠送原API文档:lettuce-core-5.0.3.RELEASE-javadoc.jar; 赠送源代码:lettuce-core-5.0.3.RELEASE-sources.jar; 赠送Maven依赖信息文件:lettuce-core-5.0.3....

    hadoop-2.10.0jar.zip

    包含hadoop平台Java开发的所有所需jar包,例如activation-1.1.jar apacheds-i18n-2.0.0-M15.jar apacheds-kerberos-codec-2.0.0-M15.jar api-asn1-api-1.0.0-M20.jar api-util-1.0.0-M20.jar asm-3.2.jar avro-1.7.7...

    xmlbeans-5.0.3-API文档-中文版.zip

    赠送原API文档:xmlbeans-5.0.3-javadoc.jar; 赠送源代码:xmlbeans-5.0.3-sources.jar; 赠送Maven依赖信息文件:xmlbeans-5.0.3.pom; 包含翻译后的API文档:xmlbeans-5.0.3-javadoc-API文档-中文(简体)版.zip; ...

    pagehelper-5.0.3.jar

    如果你也在用 MyBatis,建议尝试该分页插件,这一定是最方便使用的分页插件。分页插件支持任何复杂的单表、多表分页。

Global site tag (gtag.js) - Google Analytics