Notice
Recent Posts
Recent Comments
Link
«   2025/01   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

괴발개발 개발하다

[ Spring ] 스프링 커넥션 풀 설정(HikariCP) 본문

spring

[ Spring ] 스프링 커넥션 풀 설정(HikariCP)

괴발새발개발자 2022. 7. 8. 15:25

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 실행

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][04]커넥션 풀 설정(HikariCP)

개인 공부 후 자료를 남기기 위한 목적이기에 내용 상에 오류가 있을 수 있습니다. git주소(Oracle DB) : https://github.com/sjinjin7/blog_study git주소(MySQL DB) : https://github.com/sjinjin7/..

kimvampa.tistory.com