简体   繁体   中英

Hibernate: default transaction timeout with respect to custom @Transactional(timeout)

In my project, which is built on Spring Boot, Hibernate and PostgreSQL (with HikariCP), I would like to set up transaction timeout for the entire app -> I simply want all the transactions that take too long to be terminated because these transactions could be started by user request and it could easily end up with exhausting the entire connection pool.

I'm aware of some ways how to set up the default transaction timeout, eg by Spring Boot property spring.transaction.default-timeout or by setting defaultTimeout property on HibernateTransactionManager . Both solutions work pretty well.

However, there are many places where the long transactions are ok in the project and I would like to have no timeout there (override the default behaviour). I've tried to use @Transactional with specified timeout parameter: @Transactional(timeout = 10) on these places but it does not override the default global value which is still used instead.

The worst way how to achieve my requirement is to define a custom annotation that would override @Transactional with default timeout and use this annotation in the entire project, which I would like not to do for many reasons.

Currently, I am not aware of any other solution. Isn't there anything nicer? Thanks.

我建议将长时间运行的作业移到单独的服务应用程序中,您可以在其中设置更长的事务超时。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM