Configure HikariCP in Spring Boot with JTDS

时光总嘲笑我的痴心妄想 提交于 2019-12-05 17:40:57

You are getting below error.

Caused by: java.lang.AbstractMethodError: null at net.sourceforge.jtds.jdbc.JtdsConnection.isValid(JtdsConnection.java:2833)

Issue is that net.sourceforge.jtds.jdbc.JtdsConnection doesn't implement isValid so you need to specify a connection-test-query to ensure that isValid method isn't called. Try by adding below property in your application.properties file.

spring.datasource.hikari.connection-test-query=SELECT 1

For using multiple datasources (Spring Boot 2.0), I had to do the following to get this to work (setting spring.datasource.hikari.connection-test-query property only worked when using a single datasource):

@Configuration
public class DataConfig {
    @Bean
    @Primary
    @ConfigurationProperties(prefix="spring.datasource")
    public DataSource primaryDataSource() {
        HikariDataSource ds = (HikariDataSource) DataSourceBuilder.create().build();

        ds.setConnectionTestQuery("SELECT 1");

        return ds;
    }

    @Bean(name="secondDataSource")
    @ConfigurationProperties(prefix="spring.datasource.second")
    public DataSource secondDataSource() {
        HikariDataSource ds = (HikariDataSource) DataSourceBuilder.create().build();

        ds.setConnectionTestQuery("SELECT 1");

        return ds;
    }

    @Bean(name="primaryJdbcTemplate")
    public JdbcTemplate primaryJdbcTemplate(DataSource primaryDataSource) {
        return new JdbcTemplate(primaryDataSource);
    }

    @Bean(name="secondJdbcTemplate")
    public JdbcTemplate secondJdbcTemplate(@Qualifier("secondDataSource") DataSource secondDataSource) {
        return new JdbcTemplate(secondDataSource);
    }
}

I ran into the same problem and found the solution from this discussion. It looks like, before doing any further configurations Hikari CP tests the validity of the connection by executing a test query which is missing in this case. So as suggested by the earlier answer, you should add one test query in your application.properties file. If you are using Oracle or mySql then you can use the following query instead (as SELECT 1 doesn't work here):

spring.datasource.hikari.connection-test-query=SELECT 1 FROM DUAL

Note: DUAL is a special one-row and one-column table present in Oracle and other databases. Thus it can be readily used to execute a simple test-query.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!