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