我可以为单个查询设置JDBC超时吗? [英] Can I set a JDBC timeout for a single query?
问题描述
我在Tomcat上有一个Web应用程序,该应用程序处理数据库连接池,并使用Spring JDBCTemplate执行查询.有人要求我实现一个状态页面,该页面将由心跳过程进行监视,以确定服务器是否一切正常.
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.
为此,我想执行一个数据库查询以确定与数据库的连接是否正常.理想情况下,由于它只是从中选择1",因此我希望它能在10秒内快速返回,以指示如果DB在那段时间内没有响应,则会失败.
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.
是否可以使用原始JDBC或Spring JDBC包装器设置每个查询的超时时间?
推荐答案
如果您使用Spring管理事务,那么也可以在事务级别指定超时. @Transactional(timeout = 10)
If you are using spring to manage transactions a time out can be specified at the transaction level as well. @Transactional(timeout=10)
这篇关于我可以为单个查询设置JDBC超时吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!