This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes JDBC and the fly likes How can I distinguish between a timeout and other types of exceptions when executing a statement? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Databases » JDBC
Bookmark "How can I distinguish between a timeout and other types of exceptions when executing a statement?" Watch "How can I distinguish between a timeout and other types of exceptions when executing a statement?" New topic
Author

How can I distinguish between a timeout and other types of exceptions when executing a statement?

Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1282

Hi folks,

I' my trying to implement some piece of code which allows to execute arbitrary (prepared) statements with respect to a given timeout value. So far everthing works fine. The queries are executed, the timeouts are respected and the statements in question get cancelled with an SQLException when the timeout limit is reached.

The only problem is that using this mechanism I don't see a possibility to distungish between SQLExceptions caused by a timeout and other SQLExceptions thrown during execution of a statement. Depending on the cause for an exception the calling code of my code should get a different result so it's necessary to distinguish the cause for an exception. The only possibility I see at the moment is to rely on the error message of the exception which doesn't seem to be a really good idea.

Do you have any ideas how to handle this cleanly?

Another solution I can think about would be to use the cancel() method in Statement and use another thread to take care for correct cancellation of the statement in case of a timeout (so that not only the Java code but also the process on the database server gets cancelled). But I'm unsure how to implement this correctly.

Thanks for your help!

Marco
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874


Marco,

Isn't that throws SQLTimeoutException. SQLTimeoutException is actually subclass of SQLException , Well i think it depends on the Driver implementation though.
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1282

Hi Balu,

thanks for your answer! According to the API documentation of java.sql.Statement an unspecified SQLException is thrown but I guess you're right, depending on the vendor a more appropriate SQLTimeoutException _COULD_ be thrown. Unfortunately this is an implementation detail I didn't want to rely on.

So now I have used a workaround or maybe an even better alternative to the JDBC based approach. I don't use the query timeout of the JDBC API at all and instead execute the SQL statements with the help of a FutureTask and ExecutorService in a background thread and make use of its timeout mechanism which reports timeouts precisely!

Marco
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874




I don't use the query timeout of the JDBC API at all and instead execute the SQL statements with the help of a FutureTask and ExecutorService in a background thread and make use of its timeout mechanism which reports timeouts precisely!


that's informative as well. cheers
 
GeeCON Prague 2014
 
subject: How can I distinguish between a timeout and other types of exceptions when executing a statement?