springboot database연동
조회수 1353회
DatabaseConfiguration.java클래스
package board.configuration;
import javax.sql.DataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
@Configuration
@PropertySource("classpath:/application.properties")
public class DatabaseConfiguration {
@Bean
@ConfigurationProperties(prefix="spring.datasource.hikari")
public HikariConfig hikariConfig() {
return new HikariConfig();
}
@Bean
public DataSource dataSource() throws Exception{
DataSource dataSource=new HikariDataSource(hikariConfig());
System.out.println(dataSource.toString());
return dataSource;
}
}
application.properties파일입니다
spring.datasource.hikari.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.hikari.jdbc-url=jdbc:mysql://localhost:3306/insight?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
spring.datasource.hikari.username=제아이디
spring.datasource.hikari.password=비밀번호
spring.datasource.hikari.connection-test-query=SELECT 1
gradle파일입니다.
plugins {
id 'org.springframework.boot' version '2.2.1.RELEASE'
id 'io.spring.dependency-management' version '1.0.8.RELEASE'
id 'java'
}
group = 'com.insight'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
configurations {
developmentOnly
runtimeClasspath {
extendsFrom developmentOnly
}
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.1.1'
compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
runtimeOnly 'mysql:mysql-connector-java'
annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
annotationProcessor 'org.projectlombok:lombok'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
test {
useJUnitPlatform()
}
스크린샷
위 property설정에서 test-query날렸는데 왜 SELECT1이 뜨질않나요??
1 답변
-
먼저 하기의 링크를 읽어보시기 바랍니다.
https://github.com/brettwooldridge/HikariCP#connectionTestQuery
🔠connectionTestQuery If your driver supports JDBC4 we strongly recommend not setting this property. This is for "legacy" drivers that do not support the JDBC4 Connection.isValid() API. This is the query that will be executed just before a connection is given to you from the pool to validate that the connection to the database is still alive. Again, try running the pool without this property, HikariCP will log an error if your driver is not JDBC4 compliant to let you know. Default: none
위의 설명과 같이
connectionTestQuery
옵션은 legacy 용입니다. 질문자가 사용하는 드라이버com.mysql.cj.jdbc.Driver
는 JDBC4 드라이버 이므로 사용하면 안되는 옵션입니다. 자동으로Connection.isValid()
을 호출하여 유효여부를 확인하고 있습니다.로그상에
HikariPool-2 - Stringing... HikariPool-2 - Startcompleted.
라고 보여지므로 커넥션풀 설정은 이상없다고 할 수 있습니다.
댓글 입력