Reputation: 321
I am creating a springboot application which has data stored in a mysql database. I have used cleardb as an add-on and have deployed the app to Heroku. When I run the app (i.e heroku open), the main page (index.html) displays successfully as it does not require access to the mySQL database. However, all of the pages that do require access to the mySQL database do not display and return the error below:
There was an unexpected error (type=Internal Server Error, status=500). Failed to obtain JDBC Connection; nested exception is com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
Here is some of my code:
Databaseconfig
@Configuration
public class DatabaseConfig {
@Bean
public NamedParameterJdbcTemplate namedParameterJdbcTemplate(DataSource dataSource) {
return new NamedParameterJdbcTemplate(dataSource);
}
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost/NBA");
dataSource.setUsername("root");
dataSource.setPassword("root");
return dataSource;
}
}
pom.xml plugins
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
database_url (heroku config)
CLEARDB_DATABASE_URL: mysql://b---------9:[email protected]/heroku_f61b74207636b77?reconnect=true
DATABASE_URL: 'mysql://b---------9:[email protected]/heroku_f61b74207636b77?reconnect=true'
Some things to take note of:
My application.properties file is currently empty.
The application runs perfectly when I run it on localhost:8080.
Any and all help would be greatly appreciated.
Upvotes: 0
Views: 2390
Reputation: 152
For your case your must change the dataSource configuration according your database_url(heroku config):
dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://us-cdbr-iron-east-05.cleardb.net/heroku_f61b74207636b77?reconnect=true");
dataSource.setUsername("b05ee51c5eec59");
dataSource.setPassword("94a4e9eb");
I also suggest you to use following in your application.properties instead of using datasource bean:
spring.datasource.username=
spring.datasource.password=
spring.datasource.url=
Also very important: change your database password in the heroku and dont write it on the internet
Upvotes: 2