HikariCP는 커넥션 풀(Java와 DataSource라는 인터페이스를 통해 커넥션 풀 사용)

 

-  Java 설정을 이용해 HikariCP 설정

import javax.sql.DataSource;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

@Configuration
@ComponentSca(basePackages = {"pakage"})
public class RootConfig {
    @Bean
    public DataSource dataSource() {
        HikariConfig hikariConfig = new HikariConfig();
        hikariConfig.setDriverClassName("oracle.jdbc.driver.OracleDriver");
        hikariConfig.setJdbcUrl("jdbc:oracle:thin:@localhost:1521:XE");
        hikariConfig.setUsername("dbname");
        hikariConfig.setPassword("dbpassword");

        HikariDataSource dataSource = new HikariDataSource(hikariConfig);

        return dataSource;
    }
}

 

 

-  Test 코드

import static org.junit.Assert.fail;

import java.beans.Transient;
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.ContextConfigration;
import org.sprintframework.test.context.junit4.SprintJUnit4ClassRunner;

import lombok.setter;
import lombok.extern.log4j.Log4j;

@Runwith(SpringJUnit4ClassRunner.class)
@ContextConfigration(classes = { RootConfig.class })
@log4j
public class testcode {
    @Setter(onMethod_ = { @Autowired })
    private DataSource dataSource;
    
    @Test
    public void testConnection() {
        try(Connection con = dataSource.getConnection()) {
            log.info(con);
        } catch (Exception e) {
            fail(e.getMessage());
        }
    }
}

+ Recent posts