JDBC 연결
ojdbc6.jar 파일 경로
- SQL Developer 설치 시
C:\sqldeveloper\jdbc\lib
- 미설치 시 (DB가 설치된 폴더 내부)
C:\app\컴퓨터 이름\product\11.2.0\dbhome_1\jdbc\lib
만약 oracle.com 사이트 JDBC Driver 항목에서 다운로드 한다면, 반드시 현재 데이터베이스 버전에 맞는 드라이버를 이용해야 함 (이 차이로 엄청난 에러를 만들어 내기도 하기 때문)
http://www.oracle.com/technetwork/database/application-development/jdbc/downloads/index.html
① 프로젝트에서 오른쪽 마우스 버튼 - Build Path - Configure Build Path..
② Libraries - Add External Jars.. > ojdbc6 파일경로로 이동해서 ojdbc6 선택 - 열기 > Apply
③ Deployment Assembly - Add.. > Java Build Path Entries - Next > ojdbc6.jar - Finish > Apply and Close
// JDBC 테스트 코드
package com.spring.persistence;
import static org.junit.Assert.fail;
import java.sql.Connection;
import java.sql.DriverManager;
import org.junit.Test;
import lombok.extern.log4j.Log4j;
@Log4j
public class JDBCTests {
static {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void testConnection() {
try(Connection con = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:orcl",
"id",
"password")){
log.info(con);
} catch(Exception e) {
fail(e.getMessage());
}
}
}
커넥션 풀 (Connection Pool) _ HikariCP
여러 명의 사용자를 동시에 처리해야 하는 웹 애플리케이션의 경우, 데이터베이스 연결 시 커넥션 풀 이용
→ 스프링에 커넥션 풀 등록해서 사용
Java에서 DataSource라는 인터페이스를 통해 커넥션 풀 사용
DataSource를 통해 매번 데이터베이스와 연결하는 것이 아니라, 미리 연결을 맺어주고 반환하는 구조 이용
커넥션 풀의 종류가 다양하고 spring-jdbc 라이브러리 이용할 수도 있지만 이 포스팅에서는 HikariCP를 활용
① pom.xml
- <dependency> 추가
<!-- https://mvnrepository.com/artifact/com.zaxxer/HikariCP -->
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>2.7.4</version>
</dependency>
② root-context.xml
- Namespaces - context 체크
- 아래 코드 추가 (hikariCP 최소 설정만 한 것)
<?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:orcl"></property>
<property name="username" value="id"></property>
<property name="password" value="password"></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="com.spring.sample"></context:component-scan>
</beans>
자세한 설정은 https://github.com/brettwooldridge/HikariCP#configura-tion-knobs-baby 참고
brettwooldridge/HikariCP
光 HikariCP・A solid, high-performance, JDBC connection pool at last. - brettwooldridge/HikariCP
github.com
'Spring' 카테고리의 다른 글
[Spring Framework] REST 방식의 개념과 설정 (0) | 2020.06.17 |
---|---|
[Spring MVC] Tiles 개념과 설정 (0) | 2020.06.12 |
[Spring] 프로젝트 설정 (1) (0) | 2020.06.08 |
[Spring] 새 프로젝트 생성, 프로젝트 구조 살펴보기, 롬복 설치 (0) | 2020.06.02 |