0

I've been playing around with a Spring Boot app deployed on Heroku but I've stumbled upon an error that I can't seem to find a solution.

I'm trying to connect to a Postgres database following the Heroku tutorial (link) but I get this error over and over again:

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

Here's the config file I'm using:

spring.datasource.url=${JDBC_DATABASE_URL}
spring.datasource.driverClassName=org.postgresql.Driver
spring.datasource.username=username
spring.datasource.password=password
spring.datasource.removeAbandoned=true

And the DatabaseConfig class:

@Configuration
public class DatabaseConfig {
    @Bean @Primary
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource dataSource() {
        return DataSourceBuilder.create()
                .build();
    }
}

Can anyone point me in the right direction. What am I doing wrong?

6
  • Hi, have you correctly added the Maven dependency for the PostGreSQL JDBC driver (pom.xml) ? <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version>9.4-1201-jdbc4</version> </dependency> Commented Jul 31, 2016 at 16:46
  • Yes - I also have the Maven dependency added Commented Jul 31, 2016 at 16:52
  • Did you create and bind to a PostgreSQL service using the Heroku command-line ? $ heroku addons:add heroku-postgresql:hobby-dev Commented Jul 31, 2016 at 17:42
  • Yeap - I've followed the steps from the tutorial. Thanks @Georgesvanhoutte for taking a look. It's really frustrating that I'm stuck with this error :( Commented Jul 31, 2016 at 17:56
  • I struggled with heroku last year. Forget it. Use pivotal cloud foundry with Spring tool suite our Command line. It works perfectly. Commented Jul 31, 2016 at 17:58

1 Answer 1

2

I encountered this same exact issue and managed to solve it. The issue is not specific to Heroku, because it can be reproduced by running the app locally as well using the same configuration.

According to the stacktrace it is clear that a DataSource has not been found in the class path. According to Spring Boot documentation, found here, you can either use spring-boot-starter-jdbc or spring-boot-starter-data-jpa to automatically get tomcat-jdbc, which appears to be the preferred one in Spring Boot.

I added the following dependency to pom.xml, which solved the problem:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.