javaspring-boothsqldbembedded-databasecompact-database

Is it possible to compact an embedded HSQL DB from a Spring Boot application?


I've created a Spring Boot application with an embedded, file-based HSQL database. The data file being created is getting fairly large, especially given the usage model, so I'm wondering if there is a way it can be compacted? Either manually or automatically?

The HSQL documentation indicates that there is a SHUTDOWN COMPACT command (which might take a while, according to the documentation), but I can't figure out how to configure Spring Boot to use it.

I'm open to forcing a SHUTDOWN COMPACT when shutting down the Spring Boot application (if that's the only option), or finding a way to issue a manual "compact" command (if HSQLDB supports such a command), or any other suggestions folks might have.


Solution

  • As mentioned, previous answer is one option, but the suggestion by boly38 is cleaner, and became my preferred approach.

    @PreDestroy
    public void preDestroy() {
        try {
            jdbcTemplate.execute("SHUTDOWN COMPACT");
        } catch (DataAccessException e) {
            // do nothing
        }
    }
    

    This allows the application to control when the DB is compressed, and removes a potentially bad option from the end-user's hand.