本文介绍了使用JPA执行VACUUM FULL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PostgreSQL数据库,我想使用JPA EntityManager启动VACUUM FULL.

I'm using a PostgreSQL DB and I would like to start VACUUM FULL using JPA EntityManager.

版本1

public void doVacuum(){
  entityManager.createNativeQuery("VACUUM FULL").executeUpdate()
}

引发TransactionRequiredException

throws TransactionRequiredException

版本2

@Transactional
public void doVacuum(){
  entityManager.createNativeQuery("VACUUM FULL").executeUpdate()
}

抛出PersistenceException"VACUUM无法在事务块内运行"

throws PersistenceException "VACUUM cannot run inside a transaction block"

版本3

public void doVacuum(){
  entityManager.createNativeQuery("VACUUM FULL").getResultList()
}

执行真空,但之后我得到PersistenceException没有结果"

vacuum is performed but after that I get PersistenceException "No results"

启动此sql命令的正确方法是什么?

What is the correct way to start this sql command?

推荐答案

如Alay Hay所述,使用基础连接将起作用:

As Alay Hay mentioned, using the underlying connection will work:

public void doVacuum(){
  org.hibernate.Session session = entityManager.unwrap(org.hibernate.Session);
  org.hibernate.internal.SessionImpl sessionImpl = (SessionImpl) session;  // required because Session doesn't provide connection()
  java.sql.Connection connection = sessionImpl.connection();
  connection.prepareStatement("VACUUM FULL").execute()
}

这篇关于使用JPA执行VACUUM FULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 08:57