DataSource란?

누피밥·2025년 3월 18일

Java

목록 보기
11/11

DataSource란?

DataSource란 애플리케이션에서 데이터베이스 connection을 얻기 방법을 표준화한 인터페이스이다. 순수 JDBC를 사용하여 데이터베이스 연결시마다 connection을 생성하고 종료하는 리소스 낭비 방식을 개선하기 위해 풀링기능을 제공하는 것이 특징이다.

Connection Pool이란?
데이터베이스 연결을 위해 connection을 미리 생성해두고 관리하는 풀(pool, 저장소)을 말한다. connection이 필요할 때 Connection Pool에서 가져와 사용하고 반납하는 방식으로 데이터베이스와 연결하는 기법이다. Connection Pool에서 connection을 가져다 사용하고 반납하는 것을 풀링 기능이라고 표현한다.

DataSource 종류

1. DriverManagerDataSource

Spring 에서 기본적으로 제공하는 DataSource이다. DriverManagerDataSource는 풀링 기능을 제공하지 않기 때문에 테스트 용도로만 사용하고 상용 애플리케이션에서는 사용하지 않는다.

예제 코드

DriverManagerDataSource ds = new DriverManagerDataSource();
ds.setDriverClassName("com.mysql.cj.jdbc.Driver");
ds.setUrl("jdbc:mysql://localhost:3306/board");
ds.setUsername("test");
ds.setPassword("12345");

Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
	conn = ds.getConnection();
	stmt = conn.createStatement();
	rs = stmt.executeQuery("select * from member");
	while (rs.next()) {
		System.out.println(rs.getString(1));
	}
} catch (Exception e) {
	e.printStackTrace();
} finally {
	if(conn != null) {
		conn.close();
	}
	if(stmt != null) {
		stmt.close();
	}
	if(rs != null) {
		rs.close();
	}
}

2. BasicDataSource

Apache Commons DBCP(Database Connection Pool) 에서 제공하는 DataSource 구현체이다. 가장 기본적이고 전통적으로 사용해온 DataSource다.

예제 코드

BasicDataSource ds = new BigDataSource();
ds.setDriverClassName("com.mysql.cj.jdbc.Driver");
ds.setUrl("jdbc:mysql://localhost:3306/board");
ds.setUsername("test");
ds.setPassword("12345");

Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
	conn = ds.getConnection();
	stmt = conn.createStatement();
	rs = stmt.executeQuery("select * from member");
	while (rs.next()) {
		System.out.println(rs.getString(1));
	}
} catch (Exception e) {
	e.printStackTrace();
} finally {
	if(conn != null) {
		conn.close();
	}
	if(stmt != null) {
		stmt.close();
	}
	if(rs != null) {
		rs.close();
	}
}

3. HirakiCPDataSource

HikariCP에서 제공하는 DataSource 구현체이다. SpringBoot 2.X 버전에서 기본적으로 사용된다.

예제 코드

HikariConfig config = new HikariConfig();
config.setDriverClassName("com.mysql.cj.jdbc.Driver");
config.setUrl("jdbc:mysql://localhost:3306/board");
config.setUsername("test");
config.setPassword("12345");

HikariDataSource ds = new HikariDataSource(config);

Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
	conn = ds.getConnection();
	stmt = conn.createStatement();
	rs = stmt.executeQuery("select * from member");
	while (rs.next()) {
		System.out.println(rs.getString(1));
	}
} catch (Exception e) {
	e.printStackTrace();
} finally {
	if(conn != null) {
		conn.close();
	}
	if(stmt != null) {
		stmt.close();
	}
	if(rs != null) {
		rs.close();
	}
}

4. ComboPooledDataSource

C3P0(ConnectionPoolLibrary)에서 제공하는 DataSource 구현체이다. Hibernate에서 사용하는 DataSource이다.

예제 코드

ComboPooledDataSource ds = new ComboPooledDataSource();
ds.setDriverClassName("com.mysql.cj.jdbc.Driver");
ds.setUrl("jdbc:mysql://localhost:3306/board");
ds.setUsername("test");
ds.setPassword("12345");

Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
	conn = ds.getConnection();
	stmt = conn.createStatement();
	rs = stmt.executeQuery("select * from member");
	while (rs.next()) {
		System.out.println(rs.getString(1));
	}
} catch (Exception e) {
	e.printStackTrace();
} finally {
	if(conn != null) {
		conn.close();
	}
	if(stmt != null) {
		stmt.close();
	}
	if(rs != null) {
		rs.close();
	}
}

DataSource 구현체 비교

특징HikariCPBasicDataSource (Apache DBCP2)ComboPooledDataSource (C3P0)
속도🚀 가장 빠름🟡 보통🔴 느림
메모리 사용량✅ 가장 적음🟡 보통🔴 많음
Connection Pool 관리✅ 가장 효율적🟡 보통🔴 비효율적
자동 Connection 복구✅ 지원❌ 기본 지원 없음✅ 지원
Statement Cache✅ 지원✅ 지원✅ 지원
Spring Boot 기본 Pool✅ 기본 사용❌ 수동 설정 필요❌ 수동 설정 필요
설정의 복잡도🟡 약간 복잡✅ 가장 쉬움🔴 복잡

결론

새로운 프로젝트에서는 HikariCP 사용이 최적
기존 프로젝트에서 BasicDataSource(Apache DBCP2)를 사용하고 있다면 유지 가능
C3P0(ComboPooledDataSource)는 최신 프로젝트에서 사용하지 않는 것이 좋음

0개의 댓글