Search code examples
javamysqlspring-bootherokudatabase-connection

"Failed to configure a DataSource" error when deploying Spring Boot app to Heroku


I'm struggling to get my Spring Boot application running on Heroku. While the app works perfectly on my local machine, it throws an error on Heroku, both with heroku local web and on the dyno. The error from heroku logs is:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class
Action:
Consider the following:
If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).

Here are the relevant details:

Application Configuration (application.yaml):

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: ${db_url}
    username: ${db_user}
    password: ${db_pass}

server:
  port: 8081

Database: I'm using JawsDB for MySQL on Heroku.

Heroku Environment Variables: I've set up environment variables on Heroku and can confirm their existence with heroku config:

db_pass: password (sample value)
db_url: jdbc:mysql://hostname:3306/db_name (sample value)
db_user: user (sample value)

Database Connection: Using the above details, I can connect to the database from the terminal and have even created tables successfully.

Local Test: When hardcoding the connection details and running the app locally, I can retrieve information from the database tables without any issues.

Yet, despite these, I face the aforementioned error on Heroku. Has anyone encountered a similar issue or can point out where I might be going wrong? Any advice or pointers would be invaluable!


Solution

  • The issue was caused by missing resource configuration in the pom.xml file, specifically for the .yaml files located in the src/main/resources directory.

    To resolve this, the following resource configuration was added to the pom.xml:

    <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
        <includes>
            <include>**/*.yaml</include>
        </includes>
    </resource>
    

    After this addition, the application was able to correctly package and recognize the application.yaml file, and everything started to connect properly.