SPRING

이클립스 hikari 커넥션 풀 설정

Jonny 2022. 1. 5. 22:55

라이브러리 추가와 DataSource 설정

https://mvnrepository.com/artifact/com.zaxxer/HikariCP

링크를 통해 porm.xml에 추가할 코드를 가져온다.

root-context.xml 안에 태그 작성한다.

<!-- Root Context: defines shared resources visible to all other web components -->
    <bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property> 
        <property name="jdbcUrl" value="jdbc:mysql://localhost/root?serverTimezone=UTC"></property>
        <property name="username" value="username"></property>
        <property name="password" value="password"></property>
    </bean>

    <!-- Hikari -->
    <bean id="dataSource"
        class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
        <constructor-arg ref="hikariConfig"></constructor-arg>
    </bean>

테스트 코드를 통해 연결확인

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("file:src/main/webapp/WEB-INF/spring/root-context.xml")
@Log4j
public class DataSourceTests {

    @Setter(onMethod_ = {@Autowired} )
        private DataSource dataSource;
        @Test
        public void testConnection() {


            try(Connection con = dataSource.getConnection()){
                log.info(con);

            } catch(Exception e ) {
                fail(e.getMessage());
            }
        }
}

JUnit을 통한 실행 결과