public class HikariTester { public static void main(String[] args) throws InterruptedException { new HikariTester().run(); }
private void run() throws InterruptedException { executeVacuum(getHikariDataSource()); Thread.sleep(1000); executeVacuum(getPGSimpleDataSource()); }
private void executeVacuum(DataSource dataSource) { try ( Connection connection = dataSource.getConnection(); PreparedStatement statement = connection.prepareStatement("VACUUM FULL")) { System.out.println("Executing VACUUM for " + dataSource.getClass().getSimpleName()); statement.execute(); System.out.println("Completed VACUUM For " + dataSource.getClass().getSimpleName()); } catch (Exception e) { e.printStackTrace(); } }
private DataSource getHikariDataSource() { HikariConfig config = new HikariConfig(); config.setDataSourceClassName("org.postgresql.ds.PGSimpleDataSource"); config.setAutoCommit(false); config.setUsername("user"); config.setPassword("password"); config.addDataSourceProperty("databaseName", "test"); config.addDataSourceProperty("serverName", "localhost"); return new HikariDataSource(config); }
private DataSource getPGSimpleDataSource() { PGSimpleDataSource dataSource = new PGSimpleDataSource(); dataSource.setDatabaseName("test"); dataSource.setServerName("localhost"); dataSource.setUser("user"); dataSource.setPassword("password"); return dataSource; }}
Executing VACUUM for HikariDataSourceorg.postgresql.util.PSQLException: ERROR: VACUUM cannot run inside a transaction block at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2476) at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2189) at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:300) at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:428) at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:354) at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:169) at org.postgresql.jdbc.PgPreparedStatement.execute(PgPreparedStatement.java:158) at com.zaxxer.hikari.pool.ProxyPreparedStatement.execute(ProxyPreparedStatement.java:44) at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.execute(HikariProxyPreparedStatement.java) at HikariTester.executeVacuum(HikariTester.java:24) at HikariTester.run(HikariTester.java:15) at HikariTester.main(HikariTester.java:11)Executing VACUUM for PGSimpleDataSourceCompleted VACUUM For PGSimpleDataSource
-Brett
\set