覆盖属性文件中数据库连接的超时 [英] Overriding timeout for database connection in properties file
问题描述
我想知道是否有一种特定的方法可以在我的 Java Web 项目的属性文件中覆盖数据库连接超时?我正在使用 Hibernate、Spring 和 MySQL DB.我尝试了几个不同的属性字段并将超时时间减少到 1 毫秒,但连接仍然完成,事务仍在正确处理.
I was wondering if there is a specific way to override database connection timeout in the properties file in my Java web project? I am using Hibernate, Spring, and MySQL DB. I have tried several different property fields and reduced the timeout time to 1 millsecond, yet the connection is still completed with transactions still being processed properly.
这些是我用过没用的属性字段...
These are the property fields I have used to no avail...
spring.jpa.properties.javax.persistence.query.timeout=1
spring.jdbc.template.query-timeout=1
hibernate.c3p0.timeout=1
hibernate 是覆盖了这个超时值还是我只是设置不当?提前致谢!
Is hibernate overriding this timeout value or am I just setting it improperly? Thanks in advance!
推荐答案
假设您使用的是 Spring Boot,您可以尝试:
Assuming that you're using Spring Boot you can try:
spring.transaction.defaultTimeout=1
此属性将事务的 defaultTimeout 设置为 1 秒.
This property sets defaultTimeout for transactions to 1 second.
(查看TransactionDefinition
似乎不可能使用比秒更精确的任何东西.)
(Looking at the source code of TransactionDefinition
it seems that it is not possible to use anything more precise than seconds.)
另见:交易属性
这是Query代码>
.如果您像这样使用它,它应该可以工作:
This is a hint for Query
. It is supposed to work if you use it like this:
entityManager.createQuery("select e from SampleEntity e")
.setHint(QueryHints.SPEC_HINT_TIMEOUT, 1)
.getResultList();
记住根据 JdbcTemplate#setQueryTimeout
javadoc:
在事务级别指定超时的事务中执行时,此处指定的任何超时都将被剩余的事务超时覆盖.
Any timeout specified here will be overridden by the remaining transaction timeout when executing within a transaction that has a timeout specified at the transaction level.
<小时>
hibernate.c3p0.timeout
我怀疑这个属性指定了从连接池获取的超时时间,而不是查询执行时间
hibernate.c3p0.timeout
I suspect that this property specifies timeout for getting from the connection pool, not for a query execution
这篇关于覆盖属性文件中数据库连接的超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!