8

Spring boot can't create my Postgres datasource. The error is starting here:

at org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder.getType(DataSourceBuilder.java:138) ~[spring-boot-autoconfigure-1.5.7.RELEASE.jar:1.5.7.RELEASE]

Ultimately throwing error:

Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method 'dataSource' threw exception; nested exception is java.lang.IllegalStateException: No supported DataSource type found

I've built Spring Apps using xml configuration and Maven, but Spring Boot and Gradle are new to me. I'm used to @Autowire pulling the datasource from the config file. Based on some SO answers, I added a database config class:

import javax.sql.DataSource;

import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.PropertySource;

@Configuration
@PropertySource({ "classpath:application.properties" })
public class DatabaseConfig {

    @Bean
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource dataSource() {

        return DataSourceBuilder.create().build();

    }

}

I'm trying to autowire the datasource to my JDBC implementation:

@Component
public class JDBCRegionNameDAO implements RegionNameDAO {

    private JdbcTemplate jdbcTemplate;

    @Autowired
    public JDBCRegionNameDAO (DataSource dataSource)  {
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    }

}

My application.properties, I've checked my postgres credentials, and ensured the service is running:

spring.datasource.driver-class-name=org.postgresql.Driver

spring.datasource.url=jdbc:postgresql://localhost:5432/world_builder
spring.datasource.platform=postgres
spring.datasource.username=postgres
spring.datasource.password=postgres

My build.gradle, from what I've looked at, it seems that I have everything here that I need, but the error being thrown above suggests I'm missing something:

buildscript {
    ext {
        springBootVersion = '1.5.7.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse-wtp'
apply plugin: 'org.springframework.boot'
apply plugin: 'war'

group = 'group.group'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

configurations {
    providedRuntime
}

dependencies { 

    compile('org.springframework.boot:spring-boot-starter-actuator')
    compile('org.springframework.boot:spring-boot-starter-jdbc')
    compile('org.springframework.boot:spring-boot-starter-web')

    compile('org.springframework.boot:spring-boot-starter-thymeleaf')
    compile('org.springframework.boot:spring-boot-devtools')

    compile group: 'org.postgresql', name: 'postgresql', version: '42.1.4'

    runtime('org.postgresql:postgresql')    

    providedRuntime('org.springframework.boot:spring-boot-starter-tomcat')
    testCompile('org.springframework.boot:spring-boot-starter-test')
}
1

2 Answers 2

11

The issue was with the compile('org.springframework.boot:spring-boot-starter-jdbc') dependency. Changing this to the following solved the issue for me: compile 'org.springframework.boot:spring-boot-starter-data-jpa:1.5.6.RELEASE'

Possibly the jdbc package or one of its dependencies is no longer supported.

4

https://stackoverflow.com/a/34794085/1053362

Adding commons-dbcp2 as a dependency worked for me.

...
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-dbcp2</artifactId>
            <version>2.1.1</version>
        </dependency>
...

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.