HomeAboutMeBlogGuest
© 2025 Sejin Cha. All rights reserved.
Built with Next.js, deployed on Vercel
🚀
Random Bit Flip
/
🚀
[2기 - 우기] 4주차 RBF
/
W5D3

W5D3

원래는 드라이버매니저를 통해서 커넥션을 얻어왔다.

DataSource - 커넥션을 관리하는 주체

  • DataSource를 통해 Connection을 얻어 올 수 있다.
  • 하지만, 매번 커넥션을 생성하고 클로즈하면, 많은 리소스가 소비된다.
  • 이런 문제를 해결하기 위해 등장한 것이 ConnectionPool
  • Datasource를 통해서 커넥션 풀에서 커넥션을 가져올 수 있다.
  • close해도 실제로 끊는 것이아니라 커넥션 풀에 반환하는 것
 
DB Connection Pool을 사용해야하는 이유는?
보통 Connection Pool을 사용하는 이유가 'Connection에 드는 비용'이 크기때문이라고 하는데, 정확히 어떤 비용인지 궁금하여 찾아서 정리해봤다. 애플리케이션에서 매 쿼리마다 새로운 Connection을 생성하고 종료한다고 했을때 어떤일이 발생할까? 네트워크 관점, DBMS관점에서 드는 비용을 살펴 본 후 애플리케이션 관점에서 설명해보겠다. 네트워크 관점 DBMS와의 통신은 TCP/IP로 이루어진다. 그렇다면 이러한 TCP/IP의 연결 과정은 무엇일까? 먼저 TCP...
DB Connection Pool을 사용해야하는 이유는?
https://devkly.com/db/db-connection-pool/
DB Connection Pool을 사용해야하는 이유는?

DBCP(DataBase Connection Pool)

notion image
  • HikariCP - 가장 많이 사용하는 DBCP, 오픈소스
  • 히카리CP는Datasource 구현체
    • notion image
  • spring-boot-starter-jdbc
  • type으로 DBCP타입을 설정해준다.
notion image
 
DataSource (Java Platform SE 8 )
A factory for connections to the physical data source that this DataSource object represents. An alternative to the DriverManager facility, a DataSource object is the preferred means of getting a connection. An object that implements the DataSource interface will typically be registered with a naming service based on the Java™ Naming and Directory (JNDI) API.
DataSource (Java Platform SE 8 )
https://docs.oracle.com/javase/8/docs/api/javax/sql/DataSource.html
connectionPoolSize 같은 세팅 가능

테스트 인스턴스

  • JUnit5 는 테스트 인스턴스 생성 기본 단위가 메소드
package com.effortguy.junit5; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertTrue; public class TestInstanceAnnotation { public int i=0; @Test void test_1() { assertTrue(++i == 1); } @Test void test_2() { assertTrue(++i == 1); } }
이거 테스트 결과가 둘다 성공으로 나옴
 
  • @TestInstance(TestInstance.Lifecycle.PER_CLASS) 로 설정하면 하나의 클래스당 인스턴스 하나 생성함....
  • perMethod일 때는 @BeforeAll을 static으로 만들어야하는데, perclass는 static아니어도 됨.
  • @TestMethodOrder(MethodOrderer.OrderAnnotation.class)
 

JdbcTemplate

  • RowMapper 인터페이스 구현을 통해서 SQL의 결과를 객체에 매핑
JdbcTemplate (Spring Framework 5.3.19 API)
Can be used within a service implementation via direct instantiation with a DataSource reference, or get prepared in an application context and given to services as bean reference. Note: The DataSource should always be configured as a bean in the application context, in the first case given to the service directly, in the second case to the prepared template.
JdbcTemplate (Spring Framework 5.3.19 API)
https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/jdbc/core/JdbcTemplate.html
[Spring JDBC] JdbcTemplate의 기본 사용법 - Heee's Development Blog
Spring JDBC 기본 과정에 대해 이해한다. JdbcTemplate의 기본 예제를 통해 사용법을 익힌다. DB와의 연결을 위한 DB Server에 관한 정보(Property)를 설정한다. 설정 정보: url, driver, username, password 해당 property file에 있는 값을 place holder을 통해 DataS래ucde의 속성으로 설정한 후 해당 BasicDataSource(DataSource interface 중 하나)를 bean으로 등록한다.
[Spring JDBC] JdbcTemplate의 기본 사용법 - Heee's Development Blog
https://gmlwjd9405.github.io/2018/12/19/jdbctemplate-usage.html
[Spring JDBC] JdbcTemplate의 기본 사용법 - Heee's Development Blog

SELECT 할때는

  • query() 나queryFor~() 시리즈 사용

INSERT, UPDATE, DELETE

  • update() 사용
 
jdbc 콜백패턴. (토비의 스프링 보라고 그래서 넘겼음)