1

I am developing a web application by using the Spring Boot, Spring Cloud Config and Docker. There are 3 projects. One is springboot web, Spring Cloud Config Server and MySql Database. I have use the Spring Boot Web with Themeleaf. It's basically perform a CRUD operation. In the development phase it's working fine. But when I deploy it in the Docker Spring-Boot webapp container(springboot-thymeleaf-web-crud) is giving me the following error--


APPLICATION FAILED TO START


Description:

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).

I have created 3 container on Docker Compose file.

version: '3'

services: 
   springboot-thymeleaf-web-crud:
      image: numery/springboot-thymeleaf-web-crud:latest
      networks: 
         - employee-network
      ports:
         - 8080:8080 
      depends_on:
         - employee-database   
         - spring-cloud-config-server-employee

   spring-cloud-config-server-employee:
      image: numery/spring-cloud-config-server-employee:latest
      networks: 
         - employee-network
      ports:
         - 8888:8888

   employee-database:
      image: mysql:5
      networks:
         - employee-network
      environment:
         - MYSQL_ROOT_PASSWORD=rootpassword
         - MYSQL_DATABASE=employee_directory
      volumes:
         - /home/numery/Docker-Database/employee_directory:/var/lib/mysql

networks:
   employee-network:

SpringBoot Web Application.properties is given below

spring.application.name=employee-service-mysql
server.port=8080
spring.cloud.config.uri=http://spring-cloud-config-server- 
employee:8888
spring.profiles.active=dev

SpringBoot Config Server is providing the following properties--

spring.datasource.url: jdbc:mysql://employee- 
database:3306/employee_directory? 
useSSL=false&serverTimezone=UTC&useLegacyDatetimeCode=false
spring.datasource.username: root
spring.datasource.password: rootpassword
spring.datasource.validationQuery: SELECT 1
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

spring.tomcat.max-wait: 20000
spring.tomcat.max-active: 50
spring.tomcat.max-idle: 20
spring.tomcat.min-idle: 15

spring.jpa.properties.hibernate.dialect: 
org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.show-sql: true
spring.jpa.format-sql: true
spring.jpa.database: MYSQL
spring.jpa.hibernate.ddl-auto: create

Out of these 3 containers Mysql and Spring Cloud Config container is working fine. But for the Spring Boot Webapp(springboot-thymeleaf-web-crud) is exiting by giving the error above.

Note: I use the same datasouce(Spring Data JPA) configuration on some other SpringBoot Rest API. Which are working fine. But this is the first time I am using SpringBoot Web. Do I need to explicitly define any configuration on the data-source.

Please help!!

atline
  • 28,355
  • 16
  • 77
  • 113
Numery
  • 402
  • 1
  • 6
  • 16

2 Answers2

0

The datasource URL, that is spring.datasource.url: jdbc:mysql://employee- may be wrong. OR Should add your datasource url to your Application.properties file

The url should be in the format jdbc:oracle:thin:@hostname:portNumber/schemaName

For further details and clarification, Not using the hostname and port in jdbc url

Dilrukshi Perera
  • 917
  • 3
  • 17
  • 31
  • I have another SpringBoot Rest Api. Running in Docker. I am using following spring.datasource.url: jdbc:mysql://customer- database:3306/customerdatabase. Why this is working fine? I am not getting any error on that rest api. – Numery Jun 17 '19 at 05:31
  • @Numery, The above datasource url in your code, spring.datasource.url: jdbc:mysql://employee- you have not specified the port connecting to the database and the host name. This might be a reason for the error- _Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured._ – Dilrukshi Perera Jun 17 '19 at 05:45
  • @Perera, it's clearly mention. spring.datasource.url: jdbc:mysql://employee- database:3306/employee_directory? useSSL=false&serverTimezone=UTC&useLegacyDatetimeCode=false – Numery Jun 17 '19 at 05:50
  • @Numery, Sorry, i didnt see _database:3306/employee_directory? useSSL=false&serverTimezone=UTC&useLegacyDatetimeCode=false_ part in the code. My mistake. **Did u try adding the url to your Application.properties file?** – Dilrukshi Perera Jun 17 '19 at 05:57
  • I have added it in the appliaction.properties file. But getting the following error--------2019-06-17 01:07:45.964 ERROR 24392 --- [ main] com.zaxxer.hikari.pool.HikariPool : HikariPool-1 - Exception during pool initialization. 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. at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174) ~[mysql-connector-java-8.0.13.jar:8.0.13] – Numery Jun 17 '19 at 06:10
  • If i use- spring.datasource.url: jdbc:mysql://localhost:3306/employee_directory?useSSL=false&serverTimezone=UTC&useLegacyDatetimeCode=false. It is working fine. But to run it in the Docker I have to give the Docker container name. – Numery Jun 17 '19 at 06:13
  • If I use - spring.datasource.url: jdbc:mysql://employee-database:3306/employee_directory?useSSL=false&serverTimezone=UTC&useLegacyDatetimeCode=false. that is giving error – Numery Jun 17 '19 at 06:14
  • Try adding _spring.datasource.username: root_ and _spring.datasource.password: rootpassword_ to the Application.properties file too. – Dilrukshi Perera Jun 17 '19 at 06:19
0

I perform the following task to solve the problem--

1) We should have up and running the mysql container.

2) Inspect mysql container which ip running-- docker inspect mysqlcontainer | grep IP

3) Add the IP of that mysql container to my springboot web app--

"spring.datasource.url: jdbc:mysql://172.20.0.2:3306/employee_directory? useSSL=false&serverTimezone =UTC&useLegacyDatetimeCode=false"

4) run mvn clean, mvn install and build the SpringBoot Web image.

5) If we down the container we need to perform the same task again. Because each time Mysql container obtain the different ip.

Numery
  • 402
  • 1
  • 6
  • 16
  • Hi, I am having the same issue. I cannot use the DB's container name on spring.datasource.url My question is why is it failing for me ? I found several tutorials and all of them use whatever the container name is. Thank you – Antonio Mora Bautista Sep 18 '22 at 23:21