Home > OS >  How to correctly integrate HSQLDB with Spring Boot?
How to correctly integrate HSQLDB with Spring Boot?

Time:11-18

I saw this post : HSQLDB Swing Client screenshot

.. AND we get (all) hsqldb server logging to our spring application.

All combined in github repo. Have fun, enjoy & DON'T USE THAT in production! ;)


more links:

CodePudding user response:

Even replacing the above server component with this:

@Bean(initMethod = "start", destroyMethod = "stop")
public Server hsqlServer(@Value("classpath:/hsqldb.properties") Resource props) throws IOException, AclFormatException {
    Server bean = new org.hsqldb.server.Server();
    bean.setProperties(PropertiesLoaderUtils.loadProperties(props));
    return bean;
}

,which is analogous to this xml configuration, shows NO ISSUES (works like charm).

BUT, adding this dependency:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

breaks everything! (Context doesn't load with:)

Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set

As soon "fixing" this:

spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.HSQLDialect

We get Exception:

...Caused by: java.net.ConnectException: Connection refused: connect

..somewhere in HikariPool....


Solution/Workaround:

import java.io.IOException;
import javax.sql.DataSource;
import org.hsqldb.server.Server;
import org.hsqldb.server.ServerAcl.AclFormatException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.DependsOn;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;

@SpringBootApplication
@EnableConfigurationProperties
public class HsqldbRunnerApplication {

    public static void main(String[] args) {
        SpringApplication.run(HsqldbRunnerApplication.class, args);
    }

    @Bean(initMethod = "start", destroyMethod = "stop")
    public Server hsqlServer(@Value("classpath:/hsqldb.properties") Resource props) throws IOException, AclFormatException {
        Server bean = new Server();
        bean.setProperties(PropertiesLoaderUtils.loadProperties(props));
        return bean;
    }

    @Bean
    @DependsOn("hsqlServer") // This is important!!
    public DataSource getDataSource(
            @Autowired DataSourceProperties dsProps) {
        DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
        dataSourceBuilder.driverClassName(dsProps.getDriverClassName());
        dataSourceBuilder.url(dsProps.getUrl());
        dataSourceBuilder.username(dsProps.getUsername());
        dataSourceBuilder.password(dsProps.getPassword());
        return dataSourceBuilder.build();
    }

}

So, we need to:

  • define our own data source bean, and depend that (make it wait) on our "hsqlServer".

  • move Controller to new class(es) (which is not bad, and was only in place for "brevity"), otherwise we get circular dependencies/unset datasources.

  • Related