괴발개발 개발하다
[ Spring ] 스프링 커넥션 풀 설정(HikariCP) 본문
1. pom.xml에 HikariCP라이브러리 추가
<!-- https://mvnrepository.com/artifact/com.zaxxer/HikariCP -->
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>2.7.8</version>
</dependency>
2. DataSource 설정 방법
1) root-context.xml 에 들어갑니다.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- Root Context: defines shared resources visible to all other web components -->
<bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
<property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:XE"></property>
<property name="username" value="pky"></property>
<property name="password" value="pky"></property>
</bean>
<!-- HikariCP configuration -->
<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
<constructor-arg ref="hikariConfig"/>
</bean>
<context:component-scan base-package="org.pky.controller"></context:component-scan>
</beans>
3. 테스트
src/test/java - persistence 패키지 - DataSourceTests 클래스
1) DataSourceTests.java에 작성
package org.pky.persistence;
import java.sql.Connection;
import javax.sql.DataSource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import lombok.extern.log4j.Log4j;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("file:src/main/webapp/WEB-INF/spring/root-context.xml")
@Log4j
public class DataSourceTests {
@Autowired
private DataSource ds;
@Test
public void testConnection() {
try(Connection con = ds.getConnection()){
log.info(con);
}catch (Exception e) {
e.printStackTrace();
log.error(e.getMessage());
}
}
}
2) JUnit test 실행
3) 결과 화면
INFO : org.springframework.test.context.support.DefaultTestContextBootstrapper - Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener, org.springframework.test.context.event.EventPublishingTestExecutionListener]
INFO : org.springframework.test.context.support.DefaultTestContextBootstrapper - Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@60015ef5, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@2f54a33d, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@1018bde2, org.springframework.test.context.support.DirtiesContextTestExecutionListener@65b3f4a4, org.springframework.test.context.transaction.TransactionalTestExecutionListener@f2ff811, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener@568ff82, org.springframework.test.context.event.EventPublishingTestExecutionListener@50caa560]
INFO : com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Starting...
WARN : com.zaxxer.hikari.util.DriverDataSource - Registered driver with driverClassName=oracle.jdbc.driver.OracleDriver was not found, trying direct instantiation.
INFO : com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Start completed.
INFO : org.pky.persistence.DataSourceTests - HikariProxyConnection@681158875 wrapping oracle.jdbc.driver.T4CConnection@28a0fd6c // 이 부분 확인
INFO : com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Shutdown initiated...
INFO : com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Shutdown completed.
< 참고 >
https://kimvampa.tistory.com/57
'spring' 카테고리의 다른 글
[ Spring ] 스프링 MyBatis 라이브러리 추가, SQLSessionFactory 설정 (0) | 2022.07.08 |
---|---|
[ Spring ] 스프링 JDBC, MYBATIS 차이 (0) | 2022.07.08 |
[ Spring ] 스프링 프로젝트의 JDBC 연결 (0) | 2022.07.08 |
[ Spring ] 스프링 커넥션 풀(Connection pool) (0) | 2022.07.08 |
[ Spring ] 스프링 model.addAttribute() 메소드 (0) | 2021.11.22 |