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 setQueryTimeout not working 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 "setQueryTimeout not working" Watch "setQueryTimeout not working" New topic

setQueryTimeout not working

Sahil Sharma
Ranch Hand

Joined: Aug 27, 2003
Posts: 152

I have a requirement where in if my query execution takes more than two minutes to execute I have throw an execption and redirect user to an error page. I tried using the setQueryTimeout method of Statement but it is not working. Any other approach i can use to achieve this?

I am using Oracle 10g as my DB.

Sandeep Sanaboyina
Ranch Hand

Joined: Dec 14, 2009
Posts: 72
You can use OracleConnectionCacheManager to create a connection pool with a TimeToLiveTimeout so that any connection taken from the pool will be logically closed within that stipulated amount of time.

You can see his link for further reference:

This time out is usually not recommended as the implementation of this timeout will some times cause the JVM to hang.

If you want to do it completely in java, you can use a process similar to this-

1. Get the connection and pass it to a runnable class and start the thread.
2. In the thread, wait for two minutes and close the connection.
3. After starting the thread, call the method that executes the query. Pass the same connection to the method.
4. If in 2 mintues the method that executes the query is not completed, the connection is closed and you will get an SQLExcetption in the method. You can handle it as you like.
5. If the execution is completed, closing the connection that is already closed does not matter.

Hope it helps.

They say you have to be the first, the best or different. I say, is it too much to ask for all three.
Sahil Sharma
Ranch Hand

Joined: Aug 27, 2003
Posts: 152
thanks sandeep, i will go for the java approach that you suggested.
I agree. Here's the link:
subject: setQueryTimeout not working
It's not a secret anymore!