0

我有一个网页,允许用户输入搜索条件。使用 get 方法提交搜索表单后,控制器类读取搜索参数,将它们设置为请求属性,然后返回页面,然后调用具有数据库连接和 sql 查询的 java 类。我的问题是:当用户决定在当前搜索尚未完成时执行新搜索时,有没有办法在开始新搜索之前终止服务器上的当前搜索。

4

2 回答 2

0

You need to interrupt() the thread that is doing the search and -admitted that the search Thread is doing some Thread.sleep() in between of performing the operations (eventually you should add those) - the thread will be interrupted by an InterruptedException that you should handle to break the search processing.

Still, you need to save the Thread.currentThread() somewhere (i.e. in the session) in order to retrieve a reference by the second one.

It's not trivial, and it's not very clean; since you need to put some interruption points in your search logic (i.e. the Thread.sleep() I mentioned before) and handle the InterruptedException properly.

For sure, the easier way is to ignore that the previous thread is still running and perform a new search.

于 2013-01-15T00:06:56.333 回答
0

你真的没有在这里提供很多信息。特别是你没有写你如何连接到数据库。如果您使用的是JDBC ,您可以使用Statement.cancel()取消执行查询。此外,您应该在会话中的某处保存有关当前正在运行的查询的信息。

于 2013-01-14T22:25:27.530 回答