File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes Implementing a query time-out Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Implementing a query time-out" Watch "Implementing a query time-out" New topic

Implementing a query time-out

Claudio Gualberto
Ranch Hand

Joined: Oct 13, 2002
Posts: 47
I'm trying to implement a query execute time-out in a Servlet using JDBC.
The Servlet class throws an Thread for each query request, and waits for it's finish in a regular time. If ocurs a time-out, the main Thread interrupts the query Thread and returns an error to the client.
The problem is, what should i make with the Connection object ? ( it came from an connection pool ). If i close it, i should replace it on the pool with an just created connection. If i dont, the process in the RDBMS remains processing and slowing down the rest of the queries requests( in case of queries without WHERE clausule).
How can i quit an process of query executing with the underlying database in a clearly way.
Claudio Gualberto.
Tina Coleman
Ranch Hand

Joined: Dec 12, 2001
Posts: 150
Haven't done this before, but thought I'd do some hunting. The java.sql.Statement interface has a method called cancel(), which "Cancels this Statement object if both the DBMS and driver support aborting an SQL statement. This method can be used by one thread to cancel a statement that is being executed by another thread". Assuming your DBMS and driver support canceling a statement, looks like exactly what you're looking for.
Claudio Gualberto
Ranch Hand

Joined: Oct 13, 2002
Posts: 47
Hi, Tina
You gave me a great idea, that is:
"what if i tried to find something at the Sun's documentation ?" ... it worked !!
That's what i found :
-----From Sun's documentation-----------
public void setQueryTimeout(int seconds)
throws SQLException
Sets the number of seconds the driver will wait for a Statement object to execute to the given number of seconds. If the limit is exceeded, an SQLException is thrown.
seconds - the new query timeout limit in seconds; zero means unlimited
SQLException - if a database access error occurs
you saved a lot of my time, thanks.
Claudio Gualberto
I agree. Here's the link:
subject: Implementing a query time-out
jQuery in Action, 3rd edition