java请求action超时,怎么停止mysql继续查询数据库?

由于查询的sql过于复杂,导致前端请求action超时,页面已经停止了,但此时从数据库show processlist看,那条查询语句还在继续执行,请问下,怎么在前端超时的同时,那条查询sql也能自动被kill掉呢?不然数据库服务器的cpu一直被占着。。

撒科打诨
浏览 665回答 2
2回答

慕容森

贴个Statement.cancel()的文档:/**&nbsp; &nbsp; &nbsp;Cancels this <code>Statement</code> object if both the DBMS and&nbsp; &nbsp; &nbsp;driver support aborting an SQL statement.&nbsp; &nbsp; &nbsp;This method can be used by one thread to cancel a statement that&nbsp; &nbsp; &nbsp;is being executed by another thread.&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;@exception SQLException if a database access error occurs or&nbsp; &nbsp; &nbsp;this method is called on a closed <code>Statement</code>&nbsp; &nbsp; &nbsp;@exception SQLFeatureNotSupportedException if the JDBC driver does not support&nbsp; &nbsp; &nbsp;this method&nbsp; &nbsp;*/&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; void cancel() throws SQLException;&nbsp; &nbsp;&nbsp;根据javadoc的描述,如果DBMS和驱动都支持的话是可以使用这个api结束可以先设置超时时间:&nbsp; &nbsp; void setQueryTimeout(int seconds) throws SQLException;&nbsp; &nbsp;&nbsp;捕获这个异常然后cancel()

慕的地10843

mysql超时设置吧
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java