首页 > 解决方案 > Spring 事务和 postgres(VACUUM,在事务外部运行)

问题描述

运行 postgres 查询时,VACUUM ANALYZE my_table;我总是在使用 HikariCP 的 spring boot 中遇到相同的错误。

Caused by: org.postgresql.util.PSQLException: ERROR: VACUUM cannot run inside a transaction block

据我了解,它应该与 一起工作@Propagation.NEVER,但我无法让它工作。

/**
* Execute non-transactionally, throw an exception if a transaction exists.
* Analogous to EJB transaction attribute of the same name.
*/
NEVER(TransactionDefinition.PROPAGATION_NEVER),

如果我将 hikari 设置为auto-commit: true有效,但我不想这样做。

VACUUM不开机如何实现跑步auto-commit

标签: postgresqlspring-boothikaricp

解决方案


有两种解决方案。

最好的方法是永远不要调用VACUUM,而是调整AUTOVACUUM以适当地运行。

第二种解决方案是auto-commit在使用 PostgreSQL 时使用,但请确保在需要时使用事务范围。在 java 和 spring 的情况下,将使用 annotation @Transactional


推荐阅读