Can I set a JDBC timeout for a single query?

ぃ、小莉子 提交于 2020-01-12 13:03:20

问题


I have a web app on Tomcat, which handles DB connection pooling, and using Spring JDBCTemplate for executing queries. It's been requested that I implement a status page which will be monitored by a heartbeat process to determine if everything is healthy with the server.

As part of this, I want to do a DB query to determine if the connection to the database is ok. Ideally, since it'd just be a 'select 1 from ', I'd want it to come back fast, within 10 seconds, to indicate a failure if the DB didn't respond in that time.

However, I don't want to change my connection to time out that quickly for normal requests.

Is there a way to set a per-query timeout using either raw JDBC or Spring JDBC wrappers?


回答1:


If you are using spring to manage transactions a time out can be specified at the transaction level as well. @Transactional(timeout=10)




回答2:


Use setQueryTimeout on the Statement (or PreparedStatement) object.



来源:https://stackoverflow.com/questions/5397432/can-i-set-a-jdbc-timeout-for-a-single-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!