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());
}
}
}
'프로그래밍 > 프로젝트 정리' 카테고리의 다른 글
[오라클 클라우드]프로젝트 호스팅 서버 만들기 (0) | 2022.01.24 |
---|---|
스프링 MVC의 Controller (0) | 2022.01.19 |
스프링 MVC의 기본 구조 (0) | 2022.01.19 |
JDBC 테스트 코드 (0) | 2022.01.18 |
스프링 프레임워크 특징 (0) | 2022.01.18 |