Spring Boot와 MySQL을 연동한 상태에서 HikariCP 라이브러리를 이용해 Connection Pool을 직접 등록하고 이를 이용하여 쿼리문을 수행하는 실습을 진행하겠습니다.
의존하고 있는 라이브러리들은 아래와 같습니다.
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.testng:testng:7.1.0'
implementation 'junit:junit:4.13.1'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
// Spring에서는 jdbc 혹은 jpa를 추가하면 자동으로 Hikari CP(Connection Pool)를 사용할 수 있다.
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
runtimeOnly 'mysql:mysql-connector-java'
}
클래스는 학생(Student.class)과 과목(Major.class) 정보를 담는 두 클래스를 생성합니다.
최대한 필요한 것만 작성했습니다.
Student.java
@Getter
@Entity
@AllArgsConstructor
@NoArgsConstructor
public class Student {
@Id
private long stuNo;
private String stuName;
private String stuMajor;
private Date admissionDate;
private long majNo;
}
Major.java
@Getter
@Entity
@AllArgsConstructor
@NoArgsConstructor
public class Major {
@Id
private long majNo;
private String majName;
private int studentCnt;
}
Spring Boot에서는 Hikari Connection Pool을 지원합니다.
그래서 build.gradle의 dependencies 블록에 jpa 라이브러리를 추가해두었다면 Hikari CP 라이브러리도 포함되기 때문에 바로 사용할 수 있습니다.
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
application.yml(혹은 application .properties)에 spring.datasource.hikari 옵션으로 값을 세팅할 수도 있지만 (해당글의 중간쯤..)
라이브러리로부터 객체를 가져와서 필요한 설정 세팅 후 connection 객체를 생성하는 클래스 HikariCPDataSource를 작성해보겠습니다.
HikariCPDataSource.java
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import java.sql.Connection;
import java.sql.SQLException;
public class HikariCPDataSource {
private static final HikariConfig config = new HikariConfig();
private static HikariDataSource ds;
static {
config.setJdbcUrl("jdbc:mysql://localhost:3306/TESTDB?serverTimeZone=UTC&CharacterEncoding=UTF-8");
config.setUsername("root");
config.setPassword("0000");
config.addDataSourceProperty("cachePrepStmts", "true");
config.addDataSourceProperty("prepStmtCacheSize", "250");
config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
config.setMaximumPoolSize(10);
config.setMinimumIdle(10);
config.setValidationTimeout(2000);
config.setConnectionTimeout(5000);
config.setIdleTimeout(2000);
config.setConnectionTimeout(5000);
// get pooled connection object
ds = new HikariDataSource(config);
}
public static Connection getConnection() throws SQLException {
System.out.println("HikariCPDataSource.getConnection 호출!");
return ds.getConnection();
}
private HikariCPDataSource(HikariConfig config){}
}
H2 데이터베이스를 연결하고자 한다면 db 정보 부분을 변경해주면 됩니다.
config.setJdbcUrl("jdbc:h2:~/{DB명}");
config.setDriverClassName("org.h2.Driver");
config.setUsername("sa");
이제 Connection을 얻은 후 실행할 쿼리들을 메서드로 생성해둡니다.
HikariCPDemo.java
fetchData() : 조회 쿼리문 수행 , updateData() : 수정 쿼리문 수행
HikariCPDataSource의 getConnection()을 호출해 커넥션 객체를 얻습니다.
public class HikariCPDemo {
public static List<Major> fetchData() throws SQLException {
String sqlQuery = "select * from major";
List<Major> majors = new ArrayList<>();
ResultSet rs = null;
try {
Connection con = HikariCPDataSource.getConnection();
PreparedStatement pst = con.prepareStatement(sqlQuery);
rs = pst.executeQuery();
} catch (Exception e) {
e.printStackTrace();
}
while (rs.next()) {
Major major;
major = new Major(rs.getInt("majno"), rs.getString("majname"), rs.getInt("student_cnt")); // db 칼럼명
majors.add(major);
}
return majors;
}
public static void updateData() throws SQLException {
String updateQuery = "update major set student_cnt = 400 where majname='SCIENCE'";
List<Major> majors = new ArrayList<>();
int ret = 0;
try {
Connection con = HikariCPDataSource.getConnection();
PreparedStatement pst = con.prepareStatement(updateQuery);
ret = pst.executeUpdate(); // RETURN the row count OR 0
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("update된 row 갯수 : " + ret);
}
}
연결을 닫는 것을 진행하고 싶다면 open한 순서의 반대 순서로 close()를 진행하면 됩니다.
rs.close();
ps.close();
con.close();
이제 위 메서드를 호출할 테스트 코드를 작성하겠습니다.
fetchData() , updateData()를, fetchData() 순으로 호출했습니다.
Connection_test.java
public class Connection_test {
@Test
public void givenConnection_thenFetchDbData() throws SQLException {
List<Major> majors = HikariCPDemo.fetchData();
for (Major m : majors) {
print(m);
}
HikariCPDemo.updateData();
List<Major> updatedMajors = HikariCPDemo.fetchData();
for (Major m : updatedMajors) {
print(m);
}
}
private void print(Major major) {
System.out.println("과목번호 : " + major.getMajNo());
System.out.println("과목명 : " + major.getMajName());
System.out.println("수강학생 : " + major.getStudentCnt());
System.out.println("====================================");
}
}
출력 모습
"C:\Program Files\Java\jdk-11.0.12\bin\java.exe" -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2021.2.2\lib\idea_rt.jar=50761:C:\Program Files\JetBrains\IntelliJ IDEA 2021.2.2\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\JetBrains\IntelliJ IDEA 2021.2.2\lib\idea_rt.jar;C:\Program Files\JetBrains\IntelliJ IDEA 2021.2.2\plugins\junit\lib\junit5-rt.jar;C:\Program Files\JetBrains\IntelliJ IDEA 2021.2.2\plugins\junit\lib\junit-rt.jar;C:\IntelliJ\Java-Study\Java-Practice\build\classes\java\main;C:\IntelliJ\Java-Study\Java-Practice\build\resources\main;C:\Users\banan\.gradle\caches\modules-2\files-2.1\org.projectlombok\lombok\1.18.20\18bcea7d5df4d49227b4a0743a536208ce4825bb\lombok-1.18.20.jar;C:\Users\banan\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-starter-web\2.5.5\c09bf133d22c865d6ce4d67ff48aece7ff1defb1\spring-boot-starter-web-2.5.5.jar;C:\Users\banan\.gradle\caches\modules-2\files-2.1\org.testng\testng\7.1.0\b0bcea778fb2899aeb4014c558babea8833d180a\testng-7.1.0.jar;C:\Users\banan\.gradle\caches\modules-2\files-2.1\junit\junit\4.13.1\cdd00374f1fee76b11e2a9d127405aa3f6be5b6a\junit-4.13.1.jar;C:\Users\banan\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-starter-data-jpa\2.5.5\5ec66dab8ad9dead3322e21d1bfafe29da89f360\spring-boot-starter-data-jpa-2.5.5.jar;C:\Users\banan\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-starter-json\2.5.5\11923b2b5ca33c6c849952ed1bd47a77c9ec0a85\spring-boot-starter-json-2.5.5.jar;C:\Users\banan\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-starter\2.5.5\8657d6f1f00002e8a839fc35fe2f2cd7fc09b72a\spring-boot-starter-2.5.5.jar;C:\Users\banan\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-starter-tomcat\2.5.5\3fb58add8060ff5a6a17e08eeabca1e823c2f04a\spring-boot-starter-tomcat-2.5.5.jar;C:\Users\banan\.gradle\caches\modules-2\files-2.1\org.springframework\spring-webmvc\5.3.10\d8a26ccc0fb4788c05eca148be8ee987d554a9d2\spring-webmvc-5.3.10.jar;C:\Users\banan\.gradle\caches\modules-2\files-2.1\org.springframework\spring-web\5.3.10\31984cef4b5d38f2711cf870994c43e89619ff4e\spring-web-5.3.10.jar;C:\Users\banan\.gradle\caches\modules-2\files-2.1\com.beust\jcommander\1.72\6375e521c1e11d6563d4f25a07ce124ccf8cd171\jcommander-1.72.jar;C:\Users\banan\.gradle\caches\modules-2\files-2.1\com.google.inject\guice\4.1.0\faf9ee8ac09eafd1128091426dd367a8c0085d55\guice-4.1.0-no_aop.jar;C:\Users\banan\.gradle\caches\modules-2\files-2.1\org.yaml\snakeyaml\1.28\7cae037c3014350c923776548e71c9feb7a69259\snakeyaml-1.28.jar;C:\Users\banan\.gradle\caches\modules-2\files-2.1\org.hamcrest\hamcrest-core\2.2\3f2bd07716a31c395e2837254f37f21f0f0ab24b\hamcrest-core-2.2.jar;C:\Users\banan\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-starter-aop\2.5.5\25b36a03046a5262089d1167e29086a62afd2e9f\spring-boot-starter-aop-2.5.5.jar;C:\Users\banan\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-starter-jdbc\2.5.5\937cf9e02eeefa8f2e4c4dade226e5c4482c3207\spring-boot-starter-jdbc-2.5.5.jar;C:\Users\banan\.gradle\caches\modules-2\files-2.1\jakarta.transaction\jakarta.transaction-api\1.3.3\c4179d48720a1e87202115fbed6089bdc4195405\jakarta.transaction-api-1.3.3.jar;C:\Users\banan\.gradle\caches\modules-2\files-2.1\jakarta.persistence\jakarta.persistence-api\2.2.3\8f6ea5daedc614f07a3654a455660145286f024e\jakarta.persistence-api-2.2.3.jar;C:\Users\banan\.gradle\caches\modules-2\files-2.1\org.hibernate\hibernate-core\5.4.32.Final\99a5e10bf455337014c190e141ec631e9ff71663\hibernate-core-5.4.32.Final.jar;C:\Users\banan\.gradle\caches\modules-2\files-2.1\org.springframework.data\spring-data-jpa\2.5.5\a1ad7a3a0b7f97ad59a77ec16dac63e58ae938db\spring-data-jpa-2.5.5.jar;C:\Users\banan\.gradle\caches\modules-2\files-2.1\org.springframework\spring-aspects\5.3.10\74cb456f1f3066ba3c83df516be856dc17e2ea86\spring-aspects-5.3.10.jar;C:\Users\banan\.gradle\caches\modules-2\files-2.1\com.fasterxml.jackson.datatype\jackson-datatype-jdk8\2.12.5\6b2f79547d217ad50dfc5b57af7444a3aa583b43\jackson-datatype-jdk8-2.12.5.jar;C:\Users\banan\.gradle\caches\modules-2\files-2.1\com.fasterxml.jackson.datatype\jackson-datatype-jsr310\2.12.5\a0a9870b681a72789c5c6bdc380e45ab719c6aa3\jackson-datatype-jsr310-2.12.5.jar;C:\Users\banan\.gradle\caches\modules-2\files-2.1\com.fasterxml.jackson.module\jackson-module-parameter-names\2.12.5\2c85c2036d0851425a260c01eb5f7ddbed1eeb00\jackson-module-parameter-names-2.12.5.jar;C:\Users\banan\.gradle\caches\modules-2\files-2.1\com.fasterxml.jackson.core\jackson-databind\2.12.5\b064cf057f23d3d35390328c5030847efeffedde\jackson-databind-2.12.5.jar;C:\Users\banan\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-autoconfigure\2.5.5\350511c5612eea8df4ef0f95e2c1b3ff3ea962e8\spring-boot-autoconfigure-2.5.5.jar;C:\Users\banan\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot\2.5.5\c6335b6e1aff79b13565890ad833bb8865ae2d50\spring-boot-2.5.5.jar;C:\Users\banan\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-starter-logging\2.5.5\2731a074593f6b1089060a4af54ef8eee464d7f0\spring-boot-starter-logging-2.5.5.jar;C:\Users\banan\.gradle\caches\modules-2\files-2.1\jakarta.annotation\jakarta.annotation-api\1.3.5\59eb84ee0d616332ff44aba065f3888cf002cd2d\jakarta.annotation-api-1.3.5.jar;C:\Users\banan\.gradle\caches\modules-2\files-2.1\org.springframework\spring-core\5.3.10\4e0b94b1e2d7a7399ecedef03dd676c34133625e\spring-core-5.3.10.jar;C:\Users\banan\.gradle\caches\modules-2\files-2.1\org.apache.tomcat.embed\tomcat-embed-websocket\9.0.53\e73e461aa0da6a1779bba31f90011cfbd4e4233\tomcat-embed-websocket-9.0.53.jar;C:\Users\banan\.gradle\caches\modules-2\files-2.1\org.apache.tomcat.embed\tomcat-embed-core\9.0.53\3343c5ebe9e66da2f75436569e371920e8db1d55\tomcat-embed-core-9.0.53.jar;C:\Users\banan\.gradle\caches\modules-2\files-2.1\org.apache.tomcat.embed\tomcat-embed-el\9.0.53\d2fcc31c3ffad7227fcb14ebffc1ed1ab8b7494\tomcat-embed-el-9.0.53.jar;C:\Users\banan\.gradle\caches\modules-2\files-2.1\org.springframework\spring-context\5.3.10\45945cec4c23de39faf98139ad81b59965014ff0\spring-context-5.3.10.jar;C:\Users\banan\.gradle\caches\modules-2\files-2.1\org.springframework\spring-aop\5.3.10\de0e0c07193697df7ba5db6086020447fdbcb24d\spring-aop-5.3.10.jar;C:\Users\banan\.gradle\caches\modules-2\files-2.1\org.springframework\spring-beans\5.3.10\1ff16eb107dd0411deaffa236467efed44d65c60\spring-beans-5.3.10.jar;C:\Users\banan\.gradle\caches\modules-2\files-2.1\org.springframework\spring-expression\5.3.10\7ca7f1b5f7a2a9e335f45ffaeb59995c8ab344bc\spring-expression-5.3.10.jar;C:\Users\banan\.gradle\caches\modules-2\files-2.1\javax.inject\javax.inject\1\6975da39a7040257bd51d21a231b76c915872d38\javax.inject-1.jar;C:\Users\banan\.gradle\caches\modules-2\files-2.1\aopalliance\aopalliance\1.0\235ba8b489512805ac13a8f9ea77a1ca5ebe3e8\aopalliance-1.0.jar;C:\Users\banan\.gradle\caches\modules-2\files-2.1\com.google.guava\guava\19.0\6ce200f6b23222af3d8abb6b6459e6c44f4bb0e9\guava-19.0.jar;C:\Users\banan\.gradle\caches\modules-2\files-2.1\org.hamcrest\hamcrest\2.2\1820c0968dba3a11a1b30669bb1f01978a91dedc\hamcrest-2.2.jar;C:\Users\banan\.gradle\caches\modules-2\files-2.1\org.aspectj\aspectjweaver\1.9.7\158f5c255cd3e4408e795b79f7c3fbae9b53b7ca\aspectjweaver-1.9.7.jar;C:\Users\banan\.gradle\caches\modules-2\files-2.1\org.springframework\spring-jdbc\5.3.10\206882992cbfee0dc908e17b36cf285857e3fa99\spring-jdbc-5.3.10.jar;C:\Users\banan\.gradle\caches\modules-2\files-2.1\com.zaxxer\HikariCP\4.0.3\107cbdf0db6780a065f895ae9d8fbf3bb0e1c21f\HikariCP-4.0.3.jar;C:\Users\banan\.gradle\caches\modules-2\files-2.1\org.hibernate.common\hibernate-commons-annotations\5.1.2.Final\e59ffdbc6ad09eeb33507b39ffcf287679a498c8\hibernate-commons-annotations-5.1.2.Final.jar;C:\Users\banan\.gradle\caches\modules-2\files-2.1\org.jboss.logging\jboss-logging\3.4.2.Final\e517b8a93dd9962ed5481345e4d262fdd47c4217\jboss-logging-3.4.2.Final.jar;C:\Users\banan\.gradle\caches\modules-2\files-2.1\org.javassist\javassist\3.27.0-GA\f63e6aa899e15eca8fdaa402a79af4c417252213\javassist-3.27.0-GA.jar;C:\Users\banan\.gradle\caches\modules-2\files-2.1\net.bytebuddy\byte-buddy\1.10.22\ef45d7e2cd1c600d279704f492ed5ce2ceb6cdb5\byte-buddy-1.10.22.jar;C:\Users\banan\.gradle\caches\modules-2\files-2.1\antlr\antlr\2.7.7\83cd2cd674a217ade95a4bb83a8a14f351f48bd0\antlr-2.7.7.jar;C:\Users\banan\.gradle\caches\modules-2\files-2.1\org.jboss\jandex\2.2.3.Final\d3865101f0666b63586683bd811d754517f331ab\jandex-2.2.3.Final.jar;C:\Users\banan\.gradle\caches\modules-2\files-2.1\com.fasterxml\classmate\1.5.1\3fe0bed568c62df5e89f4f174c101eab25345b6c\classmate-1.5.1.jar;C:\Users\banan\.gradle\caches\modules-2\files-2.1\org.dom4j\dom4j\2.1.3\a75914155a9f5808963170ec20653668a2ffd2fd\dom4j-2.1.3.jar;C:\Users\banan\.gradle\caches\modules-2\files-2.1\org.glassfish.jaxb\jaxb-runtime\2.3.5\a169a961a2bb9ac69517ec1005e451becf5cdfab\jaxb-runtime-2.3.5.jar;C:\Users\banan\.gradle\caches\modules-2\files-2.1\org.springframework\spring-orm\5.3.10\a010813c931ab39c6836b74e98114ddc346b9a03\spring-orm-5.3.10.jar;C:\Users\banan\.gradle\caches\modules-2\files-2.1\org.springframework.data\spring-data-commons\2.5.5\4d40ebb9fa2bc841571fd88070986f4442aa8cdb\spring-data-commons-2.5.5.jar;C:\Users\banan\.gradle\caches\modules-2\files-2.1\org.springframework\spring-tx\5.3.10\2840cabc3b3d6b993f8dbb169eb36321c97fec85\spring-tx-5.3.10.jar;C:\Users\banan\.gradle\caches\modules-2\files-2.1\org.slf4j\slf4j-api\1.7.32\cdcff33940d9f2de763bc41ea05a0be5941176c3\slf4j-api-1.7.32.jar;C:\Users\banan\.gradle\caches\modules-2\files-2.1\com.fasterxml.jackson.core\jackson-core\2.12.5\725e364cc71b80e60fa450bd06d75cdea7fb2d59\jackson-core-2.12.5.jar;C:\Users\banan\.gradle\caches\modules-2\files-2.1\com.fasterxml.jackson.core\jackson-annotations\2.12.5\52d929d5bb21d0186fe24c09624cc3ee4bafc3b3\jackson-annotations-2.12.5.jar;C:\Users\banan\.gradle\caches\modules-2\files-2.1\ch.qos.logback\logback-classic\1.2.6\b09efa852337fa0dd9859614389eec58dc287116\logback-classic-1.2.6.jar;C:\Users\banan\.gradle\caches\modules-2\files-2.1\org.apache.logging.log4j\log4j-to-slf4j\2.14.1\ce8a86a3f50a4304749828ce68e7478cafbc8039\log4j-to-slf4j-2.14.1.jar;C:\Users\banan\.gradle\caches\modules-2\files-2.1\org.slf4j\jul-to-slf4j\1.7.32\8a055c04ab44e8e8326901cadf89080721348bdb\jul-to-slf4j-1.7.32.jar;C:\Users\banan\.gradle\caches\modules-2\files-2.1\org.springframework\spring-jcl\5.3.10\747a2d6ba0d8a74c4f90a8c66c221e3084e864ee\spring-jcl-5.3.10.jar;C:\Users\banan\.gradle\caches\modules-2\files-2.1\jakarta.xml.bind\jakarta.xml.bind-api\2.3.3\48e3b9cfc10752fba3521d6511f4165bea951801\jakarta.xml.bind-api-2.3.3.jar;C:\Users\banan\.gradle\caches\modules-2\files-2.1\org.glassfish.jaxb\txw2\2.3.5\ec8930fa62e7b1758b1664d135f50c7abe86a4a3\txw2-2.3.5.jar;C:\Users\banan\.gradle\caches\modules-2\files-2.1\com.sun.istack\istack-commons-runtime\3.0.12\cbbe1a62b0cc6c85972e99d52aaee350153dc530\istack-commons-runtime-3.0.12.jar;C:\Users\banan\.gradle\caches\modules-2\files-2.1\ch.qos.logback\logback-core\1.2.6\25be1abb32e870ff042e698a799b56587e0dca9a\logback-core-1.2.6.jar;C:\Users\banan\.gradle\caches\modules-2\files-2.1\org.apache.logging.log4j\log4j-api\2.14.1\cd8858fbbde69f46bce8db1152c18a43328aae78\log4j-api-2.14.1.jar;C:\Users\banan\.gradle\caches\modules-2\files-2.1\mysql\mysql-connector-java\8.0.26\e5ec6610020a3084b7d32ee725d1650176f6b3de\mysql-connector-java-8.0.26.jar;C:\Users\banan\.gradle\caches\modules-2\files-2.1\com.sun.activation\jakarta.activation\1.2.2\74548703f9851017ce2f556066659438019e7eb5\jakarta.activation-1.2.2.jar" com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit4 com.moonz.javapractice.CP_Practice.Connection_test,givenConnection_thenFetchDbData
22:35:39.663 [main] WARN com.zaxxer.hikari.HikariConfig - HikariPool-1 - idleTimeout has been set but has no effect because the pool is operating as a fixed size pool.
22:35:39.669 [main] DEBUG com.zaxxer.hikari.HikariConfig - HikariPool-1 - configuration:
22:35:39.677 [main] DEBUG com.zaxxer.hikari.HikariConfig - allowPoolSuspension................................false
22:35:39.677 [main] DEBUG com.zaxxer.hikari.HikariConfig - autoCommit................................true
22:35:39.677 [main] DEBUG com.zaxxer.hikari.HikariConfig - catalog................................none
22:35:39.677 [main] DEBUG com.zaxxer.hikari.HikariConfig - connectionInitSql................................none
22:35:39.677 [main] DEBUG com.zaxxer.hikari.HikariConfig - connectionTestQuery................................none
22:35:39.677 [main] DEBUG com.zaxxer.hikari.HikariConfig - connectionTimeout................................5000
22:35:39.677 [main] DEBUG com.zaxxer.hikari.HikariConfig - dataSource................................none
22:35:39.677 [main] DEBUG com.zaxxer.hikari.HikariConfig - dataSourceClassName................................none
22:35:39.677 [main] DEBUG com.zaxxer.hikari.HikariConfig - dataSourceJNDI................................none
22:35:39.678 [main] DEBUG com.zaxxer.hikari.HikariConfig - dataSourceProperties................................{password=<masked>, prepStmtCacheSqlLimit=2048, cachePrepStmts=true, prepStmtCacheSize=250}
22:35:39.678 [main] DEBUG com.zaxxer.hikari.HikariConfig - driverClassName................................none
22:35:39.679 [main] DEBUG com.zaxxer.hikari.HikariConfig - exceptionOverrideClassName................................none
22:35:39.679 [main] DEBUG com.zaxxer.hikari.HikariConfig - healthCheckProperties................................{}
22:35:39.679 [main] DEBUG com.zaxxer.hikari.HikariConfig - healthCheckRegistry................................none
22:35:39.679 [main] DEBUG com.zaxxer.hikari.HikariConfig - idleTimeout................................2000
22:35:39.679 [main] DEBUG com.zaxxer.hikari.HikariConfig - initializationFailTimeout................................1
22:35:39.679 [main] DEBUG com.zaxxer.hikari.HikariConfig - isolateInternalQueries................................false
22:35:39.679 [main] DEBUG com.zaxxer.hikari.HikariConfig - jdbcUrl................................jdbc:mysql://localhost:3306/TESTDB?serverTimeZone=UTC&CharacterEncoding=UTF-8
22:35:39.679 [main] DEBUG com.zaxxer.hikari.HikariConfig - keepaliveTime................................0
22:35:39.679 [main] DEBUG com.zaxxer.hikari.HikariConfig - leakDetectionThreshold................................0
22:35:39.679 [main] DEBUG com.zaxxer.hikari.HikariConfig - maxLifetime................................1800000
22:35:39.679 [main] DEBUG com.zaxxer.hikari.HikariConfig - maximumPoolSize................................10
22:35:39.679 [main] DEBUG com.zaxxer.hikari.HikariConfig - metricRegistry................................none
22:35:39.680 [main] DEBUG com.zaxxer.hikari.HikariConfig - metricsTrackerFactory................................none
22:35:39.680 [main] DEBUG com.zaxxer.hikari.HikariConfig - minimumIdle................................10
22:35:39.680 [main] DEBUG com.zaxxer.hikari.HikariConfig - password................................<masked>
22:35:39.680 [main] DEBUG com.zaxxer.hikari.HikariConfig - poolName................................"HikariPool-1"
22:35:39.680 [main] DEBUG com.zaxxer.hikari.HikariConfig - readOnly................................false
22:35:39.681 [main] DEBUG com.zaxxer.hikari.HikariConfig - registerMbeans................................false
22:35:39.681 [main] DEBUG com.zaxxer.hikari.HikariConfig - scheduledExecutor................................none
22:35:39.681 [main] DEBUG com.zaxxer.hikari.HikariConfig - schema................................none
22:35:39.681 [main] DEBUG com.zaxxer.hikari.HikariConfig - threadFactory................................internal
22:35:39.681 [main] DEBUG com.zaxxer.hikari.HikariConfig - transactionIsolation................................default
22:35:39.681 [main] DEBUG com.zaxxer.hikari.HikariConfig - username................................"root"
22:35:39.682 [main] DEBUG com.zaxxer.hikari.HikariConfig - validationTimeout................................2000
22:35:39.683 [main] INFO com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Starting...
22:35:39.745 [main] DEBUG com.zaxxer.hikari.util.DriverDataSource - Loaded driver with class name com.mysql.cj.jdbc.Driver for jdbcUrl=jdbc:mysql://localhost:3306/TESTDB?serverTimeZone=UTC&CharacterEncoding=UTF-8
22:35:42.015 [main] DEBUG com.zaxxer.hikari.pool.HikariPool - HikariPool-1 - Added connection com.mysql.cj.jdbc.ConnectionImpl@7efaad5a
22:35:42.027 [main] INFO com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Start completed.
HikariCPDataSource.getConnection 호출!
과목번호 : 10
과목명 : SCIENCE
수강학생 : 400
====================================
과목번호 : 20
과목명 : AI
수강학생 : 150
====================================
과목번호 : 30
과목명 : KOREAN_CULTURE
수강학생 : 300
====================================
과목번호 : 40
과목명 : PHYSICS
수강학생 : 140
====================================
HikariCPDataSource.getConnection 호출!
22:35:42.127 [HikariPool-1 connection adder] DEBUG com.zaxxer.hikari.pool.HikariPool - HikariPool-1 - Added connection com.mysql.cj.jdbc.ConnectionImpl@cf1b67d
22:35:42.128 [HikariPool-1 housekeeper] DEBUG com.zaxxer.hikari.pool.HikariPool - HikariPool-1 - Pool stats (total=2, active=2, idle=0, waiting=0)
update된 row 갯수 : 1
HikariCPDataSource.getConnection 호출!
22:35:42.142 [main] DEBUG com.zaxxer.hikari.pool.HikariPool - HikariPool-1 - Add connection elided, waiting 1, queue 7
22:35:42.167 [HikariPool-1 connection adder] DEBUG com.zaxxer.hikari.pool.HikariPool - HikariPool-1 - Added connection com.mysql.cj.jdbc.ConnectionImpl@4dccbc46
과목번호 : 10
과목명 : SCIENCE
수강학생 : 500
====================================
과목번호 : 20
과목명 : AI
수강학생 : 150
====================================
과목번호 : 30
과목명 : KOREAN_CULTURE
수강학생 : 300
====================================
과목번호 : 40
과목명 : PHYSICS
수강학생 : 140
====================================
Process finished with exit code 0
HikariConfig와 HikariDataSource를 이용해
직접 필요한 설정들을 세팅하고 Connection 객체를 가져와서 쿼리문을 수행하는 메서드와
이를 호출하는 메서드를 코드로 작성해보았습니다.
Spring Boot를 이용하게 되면 이러한 부분을 개발자가 직접 작성할 필요 없습니다.
그럼에도 한번 쯤 이러한 과정을 직접 작성해보고 테스트해보는 것은 필요한 시간 이란 생각이 듭니다.
<끝>
'백엔드 개발하며 작성한 > Spring' 카테고리의 다른 글
@Component에 대해 (@Bean) (0) | 2022.07.26 |
---|---|
@RequestBody 붙였어?? (0) | 2022.07.13 |
Filter와 OncePerRequestFilter (0) | 2022.02.22 |
Spring MVC Request Lifecycle (0) | 2022.02.16 |
단위 테스트 코드 작성 (JUnit5) (1) | 2022.02.14 |