When I worked on my computer at home, it worked well without any errors, but when I downloaded the project to my laptop through git, the error occurred as below.
2022-08-01 00:12:43.419 INFO 25004 --- [ main] c.p.r.mysqlcheck.MySqlConnectionTest : The following 1 profile is active: "config"
***************************
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
2
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 (the profiles config are currently active).
I search on google and it saids that this error occur because of wrong data source information. But I checked data source several times but nothing's wrong.
So I think config profile file is not registered properly.
How can I solve this problem??
I'll show my code below.
application.properties
#Important information will be placed in here
spring.profiles.include=config
## I'm going to hide this file beacause it has personal information
#mybatis mapper location
mybatis.type-aliases-package=com.prac.react.model.dto
mybatis.mapper-locations=classpath:mappers/*.xml
mybatis.configuration.map-underscore-to-camel-case=true
application-config.properties
spring.datasource.url=jdbc:mysql://localhost:3306/kculter?serverTimezone=UTC&allowPublicKeyRetrieval=true
spring.datasource.username=id
spring.datasource.password=1234
##db를 로컬 서버로 돌렸고 username과 pwd는 제 로컬에서 사용하는걸로 변경했습니다.
##db 서버 연결 테스트코드를 위한 변수를 만들었습니다.
mysqlusername=id
pwd=1234
Test Code
@SpringBootTest
public class MySqlConnectionTest {
private final String DRIVER = "com.mysql.jdbc.Driver"; //mysql 드라이버 생성 주소?
private final String URL = "jdbc:mysql://localhost:3306/kculter"; //mysql 주소
@Autowired
Mysql mysql;
Logger logger = LoggerFactory.getLogger(MySqlConnectionTest.class);
@Test
@DisplayName("MySql 연결 확인 테스트")
public void testConnection() throws Exception{
boolean flag = true;
logger.info(mysql.toString());
Class.forName(DRIVER); //위에서의 정보르들을 가지고 해당 driver를 JVM에 등록시키는것
try(Connection con = DriverManager.getConnection(URL,mysql.getUsername(),mysql.getPwd())){
logger.info(con.toString()); //콘솔창에서 연결정보
}catch(Exception e) {
logger.error("연결 실패");
flag = false;
assertTrue(flag);
}
}
}
Mysql.java
package com.prac.react.model.dto;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class Mysql {
@Value("${mysqlusername}")
private String username;
@Value("${pwd}")
private String pwd;
public Mysql() {}
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPwd() {
return this.pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
@Override
public String toString() {
return "{"
" username='" getUsername() "'"
", pwd='" getPwd() "'"
"}";
}
}
CodePudding user response:
Reason
The JPA auto configuration feature of the spring boot application attempts to establish database connection using JPA Datasource. The JPA DataSource bean requires database driver to connect to a database.
The database driver should be available as a dependency in the pom.xml file. For the external databases such as Oracle, SQL Server, MySql, DB2, Postgres, MongoDB etc requires the database JDBC connection properties to establish the connection. The in-memory databases such as H2, HSQL, Derby etc will establish a connection without JDBC connection properties as it is part of the spring boot application.
This error happens if you have configured in your pom.xml file the spring-boot-starter-data-jpa dependency and you haven’t defined any datasource url in your application.properties
Solution
I had the same problem, I resolved it by clean install and then doing right click on the project, maven/update project.
If that does not solve your issue then make sure your application.properties contains the complete configuration. Also make sure that your pom.xml file has the JDBC Driver for your DB
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/kculter
spring.datasource.username=id
spring.datasource.password=1234
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
## Hibernate Properties
# The SQL dialect makes Hibernate generate better SQL for the chosen database