Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

setQueryTimeout not working

 
Sahil Sharma
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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.

Thanks.
 
Sandeep Sanaboyina
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sahil Sharma
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks sandeep, i will go for the java approach that you suggested.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic