amaranth.log
💻 개발

[기술부채] 우테코 5기 레벨4 - 6주차: JDBC 라이브러리 구현하기 1~3단계

코드 리뷰


JDBC 라이브러리 구현하기 미션 1, 2단계(1)

  • sql 구문의 파라미터를 셋팅할 때, 자료형이 명시되어 있지 않은 경우 setString(), setLong() 대신 setObject()를 사용할 수도 있다.
    • private void setSQLParameter(final Object parameter, final int parameterIndex, final PreparedStatement pstmt) throws SQLException { if (parameter instanceof String) { pstmt.setString(parameterIndex, (String) parameter); return; } if (parameter instanceof Long) { pstmt.setLong(parameterIndex, (Long) parameter); return; } ... if (parameter instanceof LocalDateTime) { pstmt.setTimestamp(parameterIndex, Timestamp.valueOf((LocalDateTime) parameter)); } }
      private void setSQLParameter(final Object parameter, final int parameterIndex, final PreparedStatement pstmt) throws SQLException { pstmt.setObject(parameterIndex, parameter); }
  • SQLException으로 인한 예외가 발생했을 때, RuntimeException보다 다른 예외를 사용하는 것이 좋아 보인다.(DataAccessException으로 변경)
    • public void update(final String sql, final Object... obj) { try (final Connection conn = dataSource.getConnection(); final PreparedStatement pstmt = conn.prepareStatement(sql)) { log.debug("query : {}", sql); setSqlParameter(obj, pstmt); pstmt.execute(); } catch (SQLException e) { log.error(e.getMessage(), e); throw new RuntimeException(e); } }
  • JDBC API에서 제공하는 Connection, PreparedStatement, ResultSet 모두 AutoCloseable의 구현체이기 때문에 try-with-resources를 적용할 수 있다.
  • 아래와 같은 방법으로 ResultSet 객체의 요소를 반환하도록 하면, DB에 같은 쿼리를 날려도 다른 결과가 나오는 경우가 발생할 수 있다.
    • 때문에 ResultSet의 요소가 1개만 나오도록 제약을 걸어두어야 한다.
      public <T> Optional<T> queryForObject(final String sql, final RowMapper<T> rowMapper, final Object... obj) { try (final Connection conn = dataSource.getConnection(); final PreparedStatement pstmt = conn.prepareStatement(sql)) { ... ResultSet rs = pstmt.executeQuery(); if (rs.next()) { return Optional.of(rowMapper.mapRow(rs, rs.getRow())); } return Optional.empty(); } catch (SQLException e) { log.error(e.getMessage(), e); throw new RuntimeException(e); } }
  • 함수형 인터페이스를 정의할 때, @FunctionalInterface 어노테이션을 사용하면 추후 인터페이스에 다른 메서드가 추가되는 일을 미연에 방지할 수 있다.
    • @FunctionalInterface public interface RowMapper<T> { T mapRow(ResultSet rs, int rowNum) throws SQLException; }

JDBC 라이브러리 구현하기 미션 1, 2단계(2)

  • getPreparedStatement()의 경우 특별한 로직 없이 단순히 PreparedStatement를 반환하는 메서드이니 Util성 클래스로 분리하는 것이 좋아보인다.
    • 리뷰어의 견해 : 메인 로직과 밀접하지 않고 변경될 가능성이 적은 기능의 경우 Util 클래스로 분리해도 괜찮다.
    • public <T> Optional<T> queryForObject(final String sql, final RowMapper<T> rowMapper, final Object... obj) { try (final Connection conn = dataSource.getConnection(); final PreparedStatement pstmt = getPreparedStatement(sql, obj, conn); final ResultSet rs = pstmt.executeQuery()) { log.debug("query : {}", sql); final List<T> result = new ArrayList<>(); while (rs.next()) { result.add(rowMapper.mapRow(rs, rs.getRow())); } validateResultSetSize(result); return Optional.of(result.get(0)); } catch (SQLException e) { log.error(e.getMessage(), e); throw new DataAccessException(e); } } private PreparedStatement getPreparedStatement(final String sql, final Object[] obj, final Connection conn) throws SQLException { final PreparedStatement pstmt = conn.prepareStatement(sql); setSqlParameter(obj, pstmt); return pstmt; }
  • ‘결과가 2개 이상이라 어느 것을 반환해야 할지 알 수 없는’ 상황은 예외를 던지는 것 외에 사용자에게 해당 사실을 알릴 방법이 없지만, ‘조회하려는 레코드가 존재하지 않’는 상황은 예외를 던지지 않고도 null이나 Optional.empty()을 반환함으로서 사용자에게 알릴 수 있다.
    • private <T> void validateResultSetSize(List<T> result) { if (result.isEmpty()) { throw new DataAccessException("조회하려는 레코드가 존재하지 않습니다."); } if (result.size() > VALID_RESULT_COUNT) { throw new DataAccessException("조회하려는 레코드는 2개 이상일 수 없습니다."); } }
  • 반복되는 try-with-resources 구문을 중복제거 해보는 건 어떤지?
    • 다음과 같은 함수형 인터페이스를 정의하고,
      public interface StatementExecutor<T> { T execute(final ResultSet rs) throws SQLException; }
      try-catch문을 메서드로 분리해주면 try-catch문에 대한 중복을 제거할 수 있다.
      public <T> List<T> tryCatchTemplate(final StatementExecutor<List<T>> executor, final String sql, final Object... obj) { try (final Connection conn = dataSource.getConnection(); final PreparedStatement pstmt = getPreparedStatement(sql, obj, conn); final ResultSet rs = pstmt.executeQuery()) { return executor.execute(rs); } catch (final SQLException exception) { log.error(exception.getMessage(), exception); throw new DataAccessException(exception); } }
      커스텀 함수형 인터페이스의 메서드 시그니쳐에 SQLException를 표기해주면, ResultSet 객체에 대한 로직을 수행하고 있는 메서드 convertResultSetToInstances()에 대해 예외 처리를 해주지 않아도 된다.
      public <T> List<T> query(final String sql, final RowMapper<T> rowMapper, final Object... obj) { return tryCatchTemplate(rs -> { log.debug("query : {}", sql); return convertResultSetToInstances(rowMapper, rs); }, sql, obj); }

JDBC 라이브러리 구현하기 3단계(1)

  • 아래와 같이 코드를 작성한 경우, try 구문에 정의된 구문이 다른 update()에는 적용할 수 없다. update, query, queryForObject에 범용적으로 사용할 수 있게 만들 수는 없을까?
    • public <T> List<T> tryCatchTemplate(final StatementExecutor<List<T>> executor, final String sql, final Object... obj) { try (final Connection conn = dataSource.getConnection(); final PreparedStatement pstmt = getPreparedStatement(sql, obj, conn); final ResultSet rs = pstmt.executeQuery()) { return executor.execute(rs); } catch (final SQLException exception) { log.error(exception.getMessage(), exception); throw new DataAccessException(exception); } }
  • Connection을 주입받는 메서드가 필요해짐에 따라 update() 메서드를 오버로딩해주었는데, queryForObject()와 query()까지 오버로딩해줄 것인지?
    • 오버로딩 없이 할 수 있는 방법은 없을까?
      public void update(final String sql, final Object... obj) { try (final Connection conn = dataSource.getConnection(); final PreparedStatement pstmt = getPreparedStatement(sql, obj, conn)) { log.debug("query : {}", sql); pstmt.execute(); } catch (SQLException exception) { log.error(exception.getMessage(), exception); throw new DataAccessException(exception); } } public void update(final Connection conn, final String sql, final Object... obj) { try (final PreparedStatement pstmt = getPreparedStatement(sql, obj, conn)) { log.debug("query : {}", sql); pstmt.execute(); } catch (SQLException exception) { log.error(exception.getMessage(), exception); throw new DataAccessException(exception); } }
      Connection을 static 객체로 다룰 수 있게 함으로써 JdbcTemplate 메서드에 직접 Connection 객체를 파라미터로 넘기지 않아도 되게끔 수정하였다.
      private <T> T tryCatchTemplate(final StatementExecutor<T> executor, final String sql, final Object... obj) { final Connection conn = DataSourceUtils.getConnection(dataSource); try (final PreparedStatement pstmt = getPreparedStatement(sql, obj, conn)) { log.debug("query : {}", sql); return executor.execute(pstmt); } catch (SQLException exception) { log.error(exception.getMessage(), exception); throw new DataAccessException(exception); } }
      public static Connection getConnection(DataSource dataSource) throws CannotGetJdbcConnectionException { Connection connection = TransactionSynchronizationManager.getResource(dataSource); if (connection != null) { return connection; } try { connection = dataSource.getConnection(); TransactionSynchronizationManager.bindResource(dataSource, connection); return connection; } catch (SQLException ex) { throw new CannotGetJdbcConnectionException("Failed to obtain JDBC Connection", ex); } }
📚 기술 부채22 / 31
  1. 1편[기술부채] 우테코 5기 레벨1 - 6주차: 블랙잭 미션, RDD와 try-with-resources2023년 3월 20일
  2. 2편[기술부채] 우테코 5기 레벨1 - 7주차: 체스 미션, 방어적 복사와 커맨드 패턴2023년 3월 27일
  3. 3편[기술부채] 우테코 5기 레벨1 - 8주차: 체스 미션, 테스트 더블과 커넥션 풀2023년 4월 14일
  4. 4편[기술부채] 우테코 5기 레벨2 - 1주차: 웹 자동차 경주, 스프링 MVC 매핑과 DI2023년 4월 16일
  5. 5편[기술부채] 우테코 5기 레벨2 - 2주차: @Valid와 Dirty Checking2023년 5월 1일
  6. 6편[기술부채] 우테코 5기 레벨2 - 3주차: 장바구니 미션, MockMvc vs RestAssured와 PRG 패턴2023년 5월 3일
  7. 7편[기술부채] 우테코 5기 레벨2 - 5주차: Mockito와 동적 쿼리2023년 5월 14일
  8. 8편[기술부채] 우테코 5기 레벨2 - 6주차: 지하철 미션, Profile과 JPA 연관관계2023년 5월 23일
  9. 9편[기술부채] 우테코 5기 레벨2 - 9주차: CORS Preflight와 멱등성, 필드 주입2023년 6월 12일
  10. 10편[기술부채] 우테코 5기 레벨2 - 8주차: 장바구니 협업 미션, LocalDateTime 직렬화와 시맨틱 버저닝2023년 6월 12일
  11. 11편[기술부채] 우테코 5기 레벨2 - 7주차: OSIV와 영속성 컨텍스트, CORS 인증2023년 6월 30일
  12. 12편[기술부채] 우테코 5기 레벨3 - 3주차: 오프셋·커서 페이지네이션과 @ElementCollection2023년 7월 18일
  13. 13편[기술부채] 우테코 5기 레벨3 - 7주차: Batch Insert와 @EventListener2023년 8월 12일
  14. 14편[기술부채] 우테코 5기 레벨3 - 8주차: JPQL Distinct와 MultipleBagFetchException2023년 8월 31일
  15. 15편[기술부채] 우테코 5기 레벨3 - 6주차: 동시성 이슈와 @Transient2023년 8월 31일
  16. 16편[기술부채] 우테코 5기 레벨3 - 1주차: 무결성과 정합성, Elastic Beanstalk2023년 8월 31일
  17. 17편[기술부채] 우테코 5기 레벨4 - 1주차: @TransactionalEventListener와 트랜잭션 전파2023년 9월 10일
  18. 18편[기술부채] 우테코 5기 레벨4 - 2주차: 톰캣 만들기와 Location 헤더2023년 9월 11일
  19. 19편[기술부채] 우테코 5기 레벨4 - 3주차: 로드밸런서, CQRS와 커버링 인덱스2023년 9월 18일
  20. 20편[기술부채] 우테코 5기 레벨4 - 4주차: @MVC 구현하기 미션2023년 9월 24일
  21. 21편[기술부채] 우테코 5기 레벨4 - 5주차: @MVC 구현하기 3단계와 JSON 응답2023년 10월 2일
  22. 22편[기술부채] 우테코 5기 레벨4 - 6주차: JDBC 라이브러리 구현하기 1~3단계2023년 10월 9일
  23. 23편[기술부채] 우테코 5기 레벨4 - 7주차: JDBC 라이브러리 구현하기 3단계2023년 10월 16일
  24. 24편[Spring] Spring DTO의 데이터 바인딩과 직렬화2023년 12월 5일
  25. 25편[기술부채] 클라이언트 사이드 라우팅2026년 2월 5일
  26. 26편[기술부채] 왜 리액트는 클래스를 버리고 함수를 선택했을까(feat. React Hooks)2026년 2월 12일
  27. 27편[기술부채] iframe과 postMessage2026년 2월 20일
  28. 28편[기술부채] Service Worker(서비스 워커)2026년 2월 24일
  29. 29편[기술부채] 커링의 어원과 함수형 프로그래밍2026년 3월 12일
  30. 30편[기술부채] AG-UI와 CopilotKit - 12026년 3월 27일
  31. 31편[기술부채] AG-UI와 CopilotKit - 22026년 3월 29일