【发布时间】:2019-08-25 22:27:03
【问题描述】:
在我的 Spring boot(2.0.7 RELEASE) 应用程序中,我无法在 application.properites 文件中手动设置/覆盖数据库连接的超时。我正在使用 JPA、Hibernate、Tomcat 连接池和 Postgres。
我已经彻底研究并发现了非常相似的问题:
- Overriding timeout for database connection in properties file
- JPA query timeout parameters ignored but @Transaction annotation works
我提出新问题的原因是因为上述问题都没有公认的答案,也没有确认的工作解决方案。我尝试在我的 application.properties 文件中包含每个建议的解决方案,但没有成功。
另外,如问题 2 中所述:如果我在 @Transactional 注释中添加参数 'timeout = someSeconds',连接会按预期超时,但如果我尝试在 application.properties 中提取它,它会失败并在默认时间超时.这里的问题是,我希望所有连接在给定时间内都超时,而不仅仅是事务。
我在 application.properties 中尝试过的事情(所需的超时时间为 4 秒):
- spring.jpa.properties.javax.persistence.query.timeout=4000
- spring.jdbc.template.query-timeout=4
- spring.transaction.defaultTimeout=4
- spring.datasource.tomcat.validation-query-timeout=4
我读过的资料:
- http://www.masterspringboot.com/configuration/web-server/configuring-tomcat-connection-pool-on-spring-boot
- https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html
- https://www.baeldung.com/spring-boot-tomcat-connection-pool
- https://www.objectdb.com/java/jpa/query/setting#Query_Hints_
我错过了一些财产吗?有谁知道为什么不能通过 application.properties 文件覆盖超时?
提前致谢。
【问题讨论】:
-
根据 Spring 2.0.7 属性(docs.spring.io/spring-boot/docs/2.0.7.RELEASE/reference/…),属性键是 spring.transaction.default-timeout 而不是 spring.transaction.defaultTimeout。试试吧,让我们知道。
-
感谢您的建议。不幸的是,结果是一样的——连接没有在指定的时间内超时。此外,我正在尝试找到一个包含所有连接而不仅仅是事务的解决方案。
-
您找到解决方案了吗?在这里面临同样的问题......
-
很遗憾没有。我没有发现任何新东西。
标签: java spring spring-boot jdbc