Java - Stop executequery() after some period

♀尐吖头ヾ 提交于 2019-12-24 02:13:35

问题


I have an SQL query executed by:

ResultSet resultSet = preparedStatement.executeQuery();

while( resultSet.next() ){
     // do some stuff
}

Is there a way to stop the execution and do some code after let's say 2 minutes of execution?

Thanks


回答1:


You can set a timeout on executing a query. SQLException will be thrown if the query doesn't complete in time and times out:

preparedstatement.setQueryTimeout(seconds);
ResultSet resultSet = preparedStatement.executeQuery();

while( resultSet.next() ){
     // do some stuff
}

Have a look at setQueryTimeout documentation



来源:https://stackoverflow.com/questions/14122768/java-stop-executequery-after-some-period

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!