Database Configuration.java Class
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;
}
}
This is the application.properties file
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=My ID
spring.datasource.hikari.password=password
spring.datasource.hikari.connection-test-query=SELECT 1
Gradle file.
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()
}
Screenshot
Why doesn't SELECT1 appear when I sent a test-query in the property setting above?
spring-boot database
Please read the link below first.
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
As described above, the connectionTestQuery
option is for legacy.
The driver com.mysql.cj.jdbc.Driver
used by the questioner is a JDBC4 driver and should not be used.
You are automatically calling Connection.isValid()
to check if it is valid.
HikariPool-2 - String... Hikari Pool-2 - Startcompleted.
so the connection pool setting is clear.
© 2024 OneMinuteCode. All rights reserved.