티스토리 뷰
반응형
● Java에서 사용되는 데이터베이스 커넥션 풀(Connection Pool) 라이브러리
● CP : Connection Pool의 약자
● build.gradle 추가
// https://mvnrepository.com/artifact/com.zaxxer/HikariCP
implementation 'com.zaxxer:HikariCP:5.0.0'
● webapp / WEB-INF / spring / 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 http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!-- 빈(bean) 정의, 데이터소스, 트랜잭션 매니저, 서비스 레이어 빈 등을 설정 -->
<!-- @Component 설정 -->
<context:component-scan base-package="com.myfirstspring.example" />
<!-- HiKariCP 설정-->
<bean name="hikariConfig" class="com.zaxxer.hikari.HikariConfig">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/testdb"/>
<property name="username" value="shop"/>
<property name="password" value="My123456789!!s"/>
<property name="connectionTimeout" value="30000"/>
<property name="minimumIdle" value="2"/>
</bean>
<bean name="dataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
<constructor-arg ref="hikariConfig"/>
</bean>
</beans>
● 테스트
○ example / DITests.java
//추가
@Autowired
DataSource dataSource; //import javax.sql.DataSource;
@Test
@DisplayName("커넥션 풀 연습")
public void testCon() {
try(Connection con = dataSource.getConnection() ) {
System.out.println(con);
} catch (SQLException e) {
fail( e.getMessage() );
}
}
'Back-End Programming > Spring' 카테고리의 다른 글
| Mybatis 설정 (0) | 2025.06.25 |
|---|---|
| 데이터베이스 연결 (0) | 2025.06.24 |
| 의존성 주입(DI) (0) | 2025.06.24 |
| Log4j2 설정 (0) | 2025.06.24 |
| spring 5 설정 (0) | 2025.06.24 |
댓글